Last active
August 29, 2015 14:07
-
-
Save kevinwright/f7a9393b58aa57f2f61d to your computer and use it in GitHub Desktop.
AutoThingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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