Created
April 4, 2015 05:12
-
-
Save iximeow/7a924996f9aa36b364a0 to your computer and use it in GitHub Desktop.
shapeless wizardry
This file contains hidden or 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
| object foo extends App { | |
| case class Elem(a: Int, b: String, c: Int) | |
| val l = Elem(1, "asf", 2) | |
| trait R[T] { | |
| def r(x: T): T | |
| } | |
| object defaultConvs extends Poly1 { | |
| implicit def default[T: R] = at[T](implicitly[R[T]].r) | |
| } | |
| def keep[T]: R[T] = new R[T] { | |
| def r(t: T) = t | |
| } | |
| implicit val stringR: R[String] = new R[String] { | |
| def r(x: String): String = { | |
| val y = x.reverse | |
| println(y) | |
| y | |
| } | |
| } | |
| implicit val intR: R[Int] = new R[Int] { | |
| def r(x: Int): Int = { | |
| val y = x * 2 | |
| println(y) | |
| y | |
| } | |
| } | |
| // ayyyyyyyyy lmao it works | |
| class R2[T, L <: HList](g: Generic[T] { type Repr = L })(implicit m: Mapper[defaultConvs.type, L]) extends R[T] { | |
| def r(x: T): T = { | |
| g.from((g.to(x) map defaultConvs).asInstanceOf[L]) | |
| } | |
| } | |
| def scrub[T: R](x: T): T = implicitly[R[T]].r(x) | |
| implicit val elemR = new R2(Generic[Elem]) | |
| val m = scrub(l) | |
| println(m) //Elem(2,fsa,4) | |
| println(l) //Elem(1,asf,2) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment