Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import $ivy.`com.chuusai::shapeless:2.3.3`
  2. import shapeless._
  3.  
  4. trait Fields[T] {
  5. def fields: List[String]
  6. override def toString: String = fields.mkString(", ")
  7. }
  8. object Fields extends LabelledProductTypeClassCompanion[Fields] {
  9. def apply[T](fs: List[String]): Fields[T] = new Fields[T] {
  10. override def fields: List[String] = fs
  11. }
  12.  
  13. implicit val string: Fields[String] = apply[String](Nil)
  14. implicit def anyVal[T <: AnyVal]: Fields[T] = apply[T](Nil)
  15.  
  16. object typeClass extends LabelledProductTypeClass[Fields] {
  17. override def product[H, T <: HList](name: String, ch: Fields[H], ct: Fields[T]) : Fields[H :: T] =
  18. Fields(name :: ct.fields)
  19.  
  20. override def emptyProduct : Fields[HNil] = Fields(Nil)
  21.  
  22. override def project[F, G](instance: => Fields[G], to: F => G, from: G => F) = Fields(instance.fields)
  23. }
  24. }
  25.  
  26. case class Foo(bar: String, baz: Boolean)
  27.  
  28. println(implicitly[Fields[Foo]].fields)
Add Comment
Please, Sign In to add comment