简体   繁体   English

Scala无形:如何将hlist.Mapper转换为案例类?

[英]Scala shapeless: how to convert a hlist.Mapper to a case class?

I'm brand new to shapeless and I would like to transform a Mapper[mix.type, HNil]#Out to a case class 我是无形的新手,我想将Mapper[mix.type, HNil]#Out转换为案例类

How can I do this? 我怎样才能做到这一点? (Let me know if you need more infos...) (让我知道您是否需要更多信息...)

That would only work if you define a case class that has the exact same shape that this Mapper#Out . 仅当您定义具有与此Mapper#Out 形状完全相同的形状的case类时,这才起作用。 If that's the case, you can create an instance of your case class using shapeless.Generic : 如果是这样,您可以使用shapeless.Generic创建您的case类的实例:

val mout = ... // HList coming from your Mapper
case class A(i: Int, s: String)
shapeless.Generic[A].from(mout): A

That's assuming Generic#Repr and Mapper[mix.type, HNil]#Out are the same type, which you can check using the following: 假设Generic#ReprMapper[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM