我正在尝试shapeless
并正在尝试了解Generic
Coproducts。这是我尝试过的:
object ShapelessExperiments {
final case class Test1()
final case class Test2()
final case class Test3()
type Test = Test1 :+: Test2 :+: Test3 :+: CNil
val t1: Test = Inr(Inl(Test2())) //fine
val t2: Test = Generic[Test].to(Test2()) //compile error
}
我希望val t2
与 完全相同val t1
,但不幸的是它甚至没有编译:
Error:(13, 25) could not find implicit value for parameter gen: shapeless.Generic[com.test.ShapelessExperiments.Test]
val t2: Test = Generic[Test].to(Test2())
Error:(13, 25) not enough arguments for method apply: (implicit gen: shapeless.Generic[com.test.ShapelessExperiments.Test])shapeless.Generic.Aux[com.test.ShapelessExperiments.Test,gen.Repr] in object Generic.
Unspecified value parameter gen.
val t2: Test = Generic[Test].to(Test2())
这个错误确实不是很清楚,对我有帮助。你能解释一下最后一个案例出了什么问题吗?