Scala 无形:如何将 hlist.Mapper 转换为大小写 class?
Scala shapeless: how to convert a hlist.Mapper to a case class?
我是 shapeless 的新手,我想将 Mapper[mix.type, HNil]#Out
转换为案例 class
我该怎么做? (如果您需要更多信息,请告诉我...)
只有当您定义一个与此 Mapper#Out
具有完全相同 形状 的案例 class 时,这才有效。如果是这种情况,您可以使用 shapeless.Generic
:
创建案例 class 的实例
val mout = ... // HList coming from your Mapper
case class A(i: Int, s: String)
shapeless.Generic[A].from(mout): A
假设 Generic#Repr
和 Mapper[mix.type, HNil]#Out
是同一类型,您可以使用以下方法检查:
val mapper = the[Mapper[mix]]
val gen = the[Generic[A]]
implicitly[mapper#Out =:= gen#Repr] // This only complies if scalac can
// prove equality between these types
我是 shapeless 的新手,我想将 Mapper[mix.type, HNil]#Out
转换为案例 class
我该怎么做? (如果您需要更多信息,请告诉我...)
只有当您定义一个与此 Mapper#Out
具有完全相同 形状 的案例 class 时,这才有效。如果是这种情况,您可以使用 shapeless.Generic
:
val mout = ... // HList coming from your Mapper
case class A(i: Int, s: String)
shapeless.Generic[A].from(mout): A
假设 Generic#Repr
和 Mapper[mix.type, HNil]#Out
是同一类型,您可以使用以下方法检查:
val mapper = the[Mapper[mix]]
val gen = the[Generic[A]]
implicitly[mapper#Out =:= gen#Repr] // This only complies if scalac can
// prove equality between these types