我试图通过shapeless
生成对象
使用此代码段:
import shapeless._
case class T1(i: Int, v: String)
class WorkbenchSpec extends FlatSpec with Matchers {
"Gen tests" should "do smth interesting" in {
println(ObjectGenerators.gen(classOf[T1]))
}
}
object FieldGen extends Poly0 {
implicit val genInt = at[Int](0)
implicit val genString = at[String]("")
}
object ObjectGenerators {
def gen[T, TRepr <: HList](c: Class[T])
(implicit
gen: Generic.Aux[T, TRepr],
fill: ops.hlist.FillWith[FieldGen.type, TRepr]
): T = {
gen.from(HList.fillWith[TRepr](FieldGen))
}
def fillList[T <: HList](implicit fill: ops.hlist.FillWith[FieldGen.type, T]) =
fill()
}
我收到错误:
找不到参数fillWith的隐含值: shapeless.ops.hlist.FillWith [com.media.gr.core.ObjectGenerators.FieldGen.type,gen.Repr] gen.from(HList.fillWithgen.Repr)
但如果我在sbt
控制台:
case class T1(i: Int, v: String)
object FieldGen extends Poly0 {
implicit val genInt = at[Int](0)
implicit val genString = at[String]("")
}
def gen[T, TRepr <: HList](c: Class[T])
(implicit
gen: Generic.Aux[T, TRepr],
fill: ops.hlist.FillWith[FieldGen.type, TRepr]
): T = {
gen.from(HList.fillWith[TRepr](FieldGen))
}
我得到生成的结果。我做错了什么?