基本上,我希望能够在string.Split(char[])不将char数组实际定义为单独变量的情况下使用.我知道你可以做其他语言string.split([' ', '\n']);或类似的东西.我怎么用C#做这个?
我编写了一个扩展Iterator [A]的自定义特性,我希望能够使用我在另一个方法返回的迭代器[A]上编写的方法.这可能吗?
trait Increment[+A] extends Iterator[A]{
def foo() = "something"
}
class Bar( source:BufferedSource){
//this ain't working
def getContents():Increment[+A] = source getLines
}
Run Code Online (Sandbox Code Playgroud)
我仍然试图绕过整个隐含的东西而不是在Bar对象定义中编写方法.我将如何以上述方式包装这样的物品?
我想定义一个适用于所有类型的子类型的通用隐式转换器T.例如:
abstract class Price[A] {
def price(a: Any): Int
}
trait Car
case class Prius(year: Int) extends Car
trait Food
case class FriedChicken() extends Food
object Def {
implicit def carToPrice[A <: Car](car: A): Price[A] = new Price[A] {
def price(car: Any) = 100
}
implicit def foodToPrice[A <: Food](food: A): Price[A] = new Price[A] {
def price(food: Any) = 5
}
// implicit object PriusPrices extends Price[Prius] {
// def price(car: Any) = 100
// }
// …Run Code Online (Sandbox Code Playgroud) 在Scala中,重载和隐式参数解析的交互似乎使得无法使以下代码可用.
trait Bijection[A, B] extends Function1[A, B] with Unapply[A, B] { self =>
def apply(a: A): B
def unapply(b: B): A
}
sealed trait Unapply[A, B] {
def unapply(b: B): A
}
object Bijection {
implicit def biject[A](a: A): Biject[A] = new Biject(a)
implicit object IntStringBijection extends Bijection[Int, String] {
override def apply(a: Int): String = a.toString
override def unapply(b: String): Int = b.toInt
}
}
sealed class Biject[A](a: A) {
def as[B](implicit f: Function1[A, B]): B = f(a)
def …Run Code Online (Sandbox Code Playgroud) 我想为双精度数组定义一些隐式方法,以使我的代码更清晰.理想情况下,它们看起来像这样:
type Vec = Array[Double]
implicit def enrichVec(v: Vec) = new {
def /(x: Double) = v map (_/x)
def *(u: Vec) = (v zip u) map {case (x,y) => x*y} sum
def normalize = v / math.sqrt(v * v)
}
Run Code Online (Sandbox Code Playgroud)
但是,该normalize函数无法正常工作,因为Scala不会递归地应用隐式方法.具体来说,我收到了一个错误Note: implicit method enrichVec is not applicable here because it comes after the application point and it lacks an explicit result type.我可以通过明确写出代码来避免这种情况normalize,但那会很难看.有更好的解决方案吗?
在Scala中,我想做:
class Identifier(val str: String) {
override def toString(): String = str
}
class Variable(t: Type, name: Identifier, mutable: Boolean) {
override def toString(): String = name
}
Run Code Online (Sandbox Code Playgroud)
但我不能,因为Scala不会隐式地将namein Variable#toString()的定义转换为String.有没有办法实现这一目标?
要明确:我不想定义一个额外的方法,如:
object Identifier {
implicit def idToString(x: Identifier): String = x.str
}
Run Code Online (Sandbox Code Playgroud)
我想要toString()调用方法来进行转换.
当我试图DBObject通过插入批量加载一个列表时,我得到了no implicit view available.
collection.insert(listObjects) // listObjects is a List[DBObject]
[error]Test.scala:139: No implicit view available from List[com.mongodb.casba
h.Imports.DBObject] => com.mongodb.casbah.Imports.DBObject.
Run Code Online (Sandbox Code Playgroud)
这个错误是什么意思?我怎么解决?
参考:
def insert [A] (docs: List[A])(implicit arg0: (A) ? DBObject) : WriteResult
下面的代码片段取自此 ScalaZ教程.
10.truthy在代码示例的底部进行评估时,我无法弄清楚如何应用隐式解析规则.
我认为 - 我明白的事情如下:
1)隐式值intCanTruthy是匿名子类的一个实例,CanTruthy[A]它根据以下内容定义-s 的truthys方法Int:
scala> implicit val intCanTruthy: CanTruthy[Int] = CanTruthy.truthys({
case 0 => false
case _ => true
})
intCanTruthy: CanTruthy[Int] = CanTruthy$$anon$1@71780051
Run Code Online (Sandbox Code Playgroud)
2)toCanIsTruthyOps隐式转换方法在评估时在范围内10.truthy,因此编译器在看到Int没有方法时会尝试使用这种隐式转换truthy方法.因此编译器将尝试寻找一些隐式转换方法,该方法转换10为具有方法的对象,truthy因此它将尝试toCanIsTruthyOps进行此转换.
3)我怀疑intCanTruthy当编译器尝试toCanIsTruthyOps隐式转换时,可能会以某种方式使用隐式值10.
但这是我真正迷失的地方.我只是没有看到隐含的解决方法过程如何进行.接下来发生什么 ?怎么样,为什么?
换句话说,我不知道什么是允许编译器truthy在评估时找到方法实现的隐式解析序列10.truthy.
问题:
如何10转换为具有正确truthy方法的某个对象?
那个对象是什么?
那个对象来自哪里?
有人可以详细解释在评估时如何进行隐式解决方案10.truthy …
我尝试构建一个简单的隐式类Int来为Ints添加一个函数:
object Helper {
implicit class IntHelper(i: Int) {
def add(str: String): Int = i + str.toInt
}
}
Run Code Online (Sandbox Code Playgroud)
为了更自然地写,我希望DSL允许这个(带import Helper._):
2 add "3" and add "4"
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何做这个and功能.我认为这个会起作用:
object Helper {
implicit class IntHelper(i: Int) {
def add(str: String): Int = i + str.toInt
def and: Int = i
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果没有括号,它就无法工作(事实上,"2.add("3").and.add("4")但有效,但是对于DSL来说,有太多的句号和括号).
谢谢
我需要找到一种在Haskell中隐式调用函数的方法,类似于implicit在Scala中使用函数的方法。
我已经研究了使用隐式参数和函数中{-# LANGUAGE ImplicitParams #-}所示的方法,但是如果不明确定义它,我将无法弄清楚如何实现类似的效果。
这是我的代码的简化版本
a :: Int -> Int
a n = n + 1
b :: [Char] -> Int
b cs = length cs
Run Code Online (Sandbox Code Playgroud)
我希望能够跑步
Test> a "how long" -- outputs 8, as it implicitly calls a (b "how long")
Run Code Online (Sandbox Code Playgroud)
以及
Test> a 5 -- outputs 6
Run Code Online (Sandbox Code Playgroud)