Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. load.ivy("com.chuusai" %% "shapeless" % "2.3.0")
  2.  
  3. @
  4.  
  5. import scala.reflect._
  6. import shapeless._
  7. import shapeless.record._
  8. import shapeless.labelled._
  9. import shapeless.ops.record._
  10.  
  11. case class Foo(x: String, y: Int)
  12.  
  13. val generic = LabelledGeneric[Foo]
  14.  
  15. // Get the field names of a case class with Shapeless
  16. val keys = implicitly[Keys[generic.Repr]].apply
  17.  
  18. // Now get the class objects as well
  19. trait FieldTypes[L <: HList] extends DepFn0 { type Out <: HList }
  20.  
  21. object FieldTypes {
  22. type Aux[L <: HList, Out0 <: HList] = FieldTypes[L] { type Out = Out0 }
  23.  
  24. def apply[L <: HList](implicit fieldTypes: FieldTypes[L]): Aux[L, fieldTypes.Out] = fieldTypes
  25.  
  26. implicit def hnilFieldTypes[L <: HNil]: Aux[L, HNil] = new FieldTypes[L] {
  27. type Out = HNil
  28.  
  29. def apply(): Out = HNil
  30. }
  31.  
  32. implicit def hlistFieldTypes[K, V, Rest <: HList](
  33. implicit fieldTypesRest: FieldTypes[Rest],
  34. clazz: ClassTag[V]
  35. ): Aux[FieldType[K, V] :: Rest, String :: fieldTypesRest.Out] = new FieldTypes[FieldType[K, V] :: Rest] {
  36. type Out = String :: fieldTypesRest.Out
  37.  
  38. def apply(): Out = clazz.runtimeClass.getName :: fieldTypesRest()
  39. }
  40. }
  41.  
  42. val fieldTypes = implicitly[FieldTypes[generic.Repr]].apply
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement