Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created April 15, 2015 11:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save milessabin/b1c515d5641fe31634bd to your computer and use it in GitHub Desktop.
package fommil.polys
import shapeless._
sealed trait Trait
case class Foo() extends Trait
case class Bar() extends Trait
trait Thing[T] { def thingy: String }
// https://github.com/milessabin/shapeless/issues/361
object Shapeless extends App {
implicit def foo: Thing[Foo] = new Thing[Foo] { def thingy = "foo" }
implicit def bar: Thing[Bar] = new Thing[Bar] { def thingy = "bar" }
// this is the bit I'd like to simplify...
// def thing(t: Trait): String = t match {
// case Foo() => implicitly[Thing[Foo]].thingy
// case Bar() => implicitly[Thing[Bar]].thingy
// }
// with this!
object thingPoly extends Poly1 {
implicit def thingAt[T <: Trait: Thing] = at[T](
a => implicitly[Thing[T]].thingy
)
}
def poly(t: Trait): String =
Generic[Trait].to(t).map(thingPoly).unify
val blah = poly(Foo())
println(blah)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment