Skip to content

Instantly share code, notes, and snippets.

@iximeow
Created April 4, 2015 05:12
Show Gist options
  • Select an option

  • Save iximeow/7a924996f9aa36b364a0 to your computer and use it in GitHub Desktop.

Select an option

Save iximeow/7a924996f9aa36b364a0 to your computer and use it in GitHub Desktop.
shapeless wizardry
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