Skip to content

Instantly share code, notes, and snippets.

@kevinwright
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinwright/f7a9393b58aa57f2f61d to your computer and use it in GitHub Desktop.
Save kevinwright/f7a9393b58aa57f2f61d to your computer and use it in GitHub Desktop.
AutoThingy
import shapeless._
trait Thingy[T] { def stringy(t:T): String }
implicit def autoThingy[T <: Product](implicit gen: LabelledGeneric[T]) : Thingy[T] = new Thingy[T] {
def toRecord[T <: Product, Repr](obj: T)(implicit lg: LabelledGeneric.Aux[T, Repr]): Repr = lg.to(obj)
def stringy(t: T) = {
val rec = toRecord(t)
val keys = rec.keys
keys.mkString
}
}
/*
error: type mismatch;
found : shapeless.LabelledGeneric[T]
required: shapeless.LabelledGeneric.Aux[T,this.Repr]
(which expands to) shapeless.LabelledGeneric[T]{type Repr = this.Repr}
val rec = toRecord(t)
^
error: value keys is not a member of this.Repr
val keys = rec.keys
^
*/
implicit def autoThingy2[T <: Product, Repr](implicit gen: LabelledGeneric.Aux[T,Repr]) : Thingy[T] = new Thingy[T] {
def stringy(t: T) = {
val rec = gen.to(t)
val keys = rec.keys
keys.mkString
}
}
/*
error: value keys is not a member of type parameter gen.Repr
val keys = rec.keys
^
*/
implicit def autoThingy3[T <: Product] : Thingy[T] = new Thingy[T] {
def toRecord[Repr](t: T)(implicit gen: LabelledGeneric.Aux[T,Repr]): Repr = gen.to(t)
def stringy(t: T) = {
val rec = toRecord(t)
val keys = rec.keys
keys.mkString
}
}
/*
error: could not find implicit value for parameter gen: shapeless.LabelledGeneric.Aux[T,Repr]
val rec = toRecord(t)
^
*/
implicit def autoThingy3[T <: Product : LabelledGeneric] : Thingy[T] = new Thingy[T] {
def toRecord[Repr](t: T)(implicit gen: LabelledGeneric.Aux[T,Repr]): Repr = gen.to(t)
def stringy(t: T) = {
val rec = toRecord(t)
val keys = rec.keys
keys.mkString
}
}
/*
error: type mismatch;
found : shapeless.LabelledGeneric[T]
required: shapeless.LabelledGeneric.Aux[T,this.Repr]
(which expands to) shapeless.LabelledGeneric[T]{type Repr = this.Repr}
val rec = toRecord(t)
^
error: value keys is not a member of this.Repr
val keys = rec.keys
^
*/
implicit def autoThingy4[T <: Product](implicit gen: LabelledGeneric[T]) : Thingy[T] = new Thingy[T] {
def toRecord[T2 <: Product, Repr](obj: T2)(implicit lg: LabelledGeneric.Aux[T2, Repr]): Repr = lg.to(obj)
def stringy(t: T) = {
val rec = toRecord(t)
val keys = rec.keys
keys.mkString
}
}
/*
error: type mismatch;
found : shapeless.LabelledGeneric[T]
required: shapeless.LabelledGeneric.Aux[T,this.Repr]
(which expands to) shapeless.LabelledGeneric[T]{type Repr = this.Repr}
val rec = toRecord(t)
^
error: value keys is not a member of this.Repr
val keys = rec.keys
^
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment