简体   繁体   中英

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

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 . If that's the case, you can create an instance of your case class using shapeless.Generic :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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