鉴于:
implicit class Foo(val i: Int) {
def addValue(v: Int): Int = i + v
}
Run Code Online (Sandbox Code Playgroud)
有可能适用于它implicitly吗?我在这里收到错误:
<console>:14: error: could not find implicit value for parameter e: Foo
implicitly[Foo]
Run Code Online (Sandbox Code Playgroud) 我有一个ADT如下:
sealed trait Tree[A]
case object EmptyTree extends Tree[Nothing]
case class Leaf[A](value: A) extends Tree[A]
case class Node[A](op: Seq[A] => A, branches: Tree[A]*) extends Tree[A]
Run Code Online (Sandbox Code Playgroud)
当我尝试构建一个随机创建树的函数时,我遇到了EmptyTree的问题,类型系统不允许通过
def create(acc: Tree[A], currentDepth: Int): Tree[A] = currentDepth match {
case maxDepth => Leaf(terminalSet(r.nextInt(terminalSet.length)))
case 0 => {
val op_pos = r.nextInt(fSetLength)
val branches: Seq[Tree[A]] = for (i <- 0 to r.nextInt(fSetLength)) yield create(EmptyTree, currentDepth+1)
Node(functionSet(op_pos)._1, branches:_*)
}
case _ => {
if (r.nextFloat() <= probF) {
val op_pos = r.nextInt(fSetLength)
val branches = …Run Code Online (Sandbox Code Playgroud) generics scala abstract-data-type covariance abstract-syntax-tree
我查询了参数化类与参数化函数之间的区别.
我提供了一个Functor的实现,如下所示:
trait Functor[F[_],A,B] {
def map(fa: F[A]) (f: A => B) : F[B]
}
Run Code Online (Sandbox Code Playgroud)
以及其他函数参数化的方法如下:
trait Functor[F[_]] {
def map[A,B](fa: F[A]) (f: A => B) : F[B]
}
Run Code Online (Sandbox Code Playgroud)
在哪里我们应该使用一个而不是另一个?
另一个跟进问题:为什么我们将参数作为F [_]而不是F [A]或F [B]传递给仿函数.当我们使用F [A]或F [B]时会出现什么情况?