#Scala那些事# HList
作者:互联网
有了HList,不用reflection,我们也可以拿到一个class的所有fields:
Import shapeless.{Generic, HNil, ::}
case class Cat(name: String, age: Int)
val gen: Generic.Aux[Cat, String :: Int :: HNil] = Generic[Cat]
val cat: Cat = Cat("kitten", 2)
val fields: String :: Int :: HNil = gen.to(cat)
val reconstructed: Cat = gen.from(fields)
是不是很神奇?!类似于Java里面用reflection来构建object,但是更加type safe。
标签:String,val,Scala,Generic,HNil,Cat,那些,HList,gen 来源: https://www.cnblogs.com/GoKubernetes/p/12382647.html