我对小猫(https://github.com/milessabin/kittens)做了一些实验,但在编译我的代码时遇到了问题。我收到以下错误。
[error] ...danirey\scala\kittens\Kittens.scala:23: could not find implicit value for parameter instance: cats.Functor[danirey.scala.kittens.AdtDefns.Tree]
[error] val funct = Functor[Tree]
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
完整文件如下
package danirey.scala.kittens
/**
* @author Dani
*/
import cats.Functor
import cats.syntax.AllSyntax
import cats.derived.functor._
import legacy._
import cats.derived.iterable.legacy._
import org.typelevel.discipline.scalatest.Discipline
import shapeless.cachedImplicit
object Kittens extends App {
val ft = new FunctorExperiment()
ft.print()
}
class FunctorExperiment extends AllSyntax {
import AdtDefns._
def print():Unit = {
val funct = Functor[Tree]
val tree: Tree[String] = Node(
Leaf("Reto"),
Node(
Leaf("Sandra"),
Leaf("Mike")
)
)
println(funct.map(tree)(_.length))
}
}
我在 ScalaTest 中使用了几乎相同的代码,它编译没有任何问题。
package danirey.scala.kittens
import cats.Functor
import cats.syntax.AllSyntax
import cats.derived.functor._
import legacy._
import cats.derived.iterable.legacy._
import org.scalatest.FunSuite
import org.typelevel.discipline.scalatest.Discipline
import shapeless.cachedImplicit
/**
* @author Dani
*/
class FunctorExperimentTest extends FunSuite with Discipline with AllSyntax {
import AdtDefns._
test("functors experiment") {
val funct = Functor[Tree]
val tree: Tree[String] = Node(
Leaf("Reto"),
Node(
Leaf("Sandra"),
Leaf("Mike")
)
)
println(funct.map(tree)(_.length))
}
}
我的 build.sbt 看起来如下 name := "shapeless-experiments"
version := "1.0-SNAPSHOT"
scalaVersion := "2.11.8"
exportJars := true
libraryDependencies ++= Seq(
"com.chuusai" % "shapeless_2.11" % "2.3.0",
"org.typelevel" % "kittens_2.11" % "1.0.0-M2",
"org.scalatest" %% "scalatest" % "3.0.0-M7" % "test"
)
scalacOptions ++= Seq(
"-feature",
"-language:higherKinds",
"-language:implicitConversions",
"-unchecked"
)
最有趣的是,它作为增量编译的一部分进行编译。
如果我注释第 16、23 和 32 行,然后执行“sbt compile”,然后再次删除注释并执行它编译的“sbt compile/package”,我什至可以执行程序。但是一旦我运行“sbt clean”,它就不再编译了。
AdtDefns 对象基本上是https://github.com/milessabin/kittens/blob/master/core/src/test/scala/cats/derived/adtdefns.scala 的副本,相关部分是
object AdtDefns {
sealed trait Tree[T]
final case class Leaf[T](t: T) extends Tree[T]
final case class Node[T](l: Tree[T], r: Tree[T]) extends Tree[T]
}
PS:如果有人可以为 scala-kittens 创建一个标签会很好