如何正确获取shapeless.Generic [T]实例

时间:2016-08-19 15:18:51

标签: scala generic-programming shapeless

我在使用Shapeless进行通用编程时遇到困难,而且我遇到了常见的初学者问题。

我实际上编写了一段代码,它将多态函数应用于编译的案例类,在Scala IDE下正常工作但无法在sbt下编译。在尝试最小化问题的同时,我在https://github.com/milessabin/shapeless/blob/master/core/src/main/scala/shapeless/generic.scala的基本示例中一路走来,而且我创建Generic实例的问题仍然存在。

我真的很感激为什么这会编译:

object Example {

  sealed class Animal

  case class Cat(name: String) extends Animal

  case class Dog(name: String) extends Animal

  val generic = shapeless.Generic[Animal]

}

虽然:

sealed class Animal

case class Cat(name: String) extends Animal

case class Dog(name: String) extends Animal

object Example {
  val generic = shapeless.Generic[Animal]
}

和此:

sealed class Animal

case class Cat(name: String) extends Animal

case class Dog(name: String) extends Animal

object Animal {
  val generic = shapeless.Generic[Animal]
}

无法编译:

could not find implicit value for parameter gen: shapeless.Generic[example.Example.Animal]
not enough arguments for method apply: (implicit gen: shapeless.Generic[example.Example.Animal])shapeless.Generic.Aux[example.Example.Animal,gen.Repr] in object Generic. Unspecified value parameter gen.

我在Scala 2.11.8中使用了无形2.3.0。

谢谢你提前!

0 个答案:

没有答案