我用小猫做了一些实验(https://github.com/milessabin/kittens)并且在编译我的代码时遇到问题。我收到以下错误。
[error] ...danireyscalakittensKittens.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:="无定形实验"
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编译/包",我甚至可以执行该程序。但一旦我运行"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小猫创建一个标签,那就太好了
@DaniRey我们在项目中使用了小猫,但只使用了序列部分。我不知道有任何项目使用小猫衍生。你的用户案例是什么?