Skip to content

Instantly share code, notes, and snippets.

@xeno-by
Created Apr 30, 2013
Embed
What would you like to do?
22:06 ~/Projects/Kepler_5923/sandbox (ticket/5923)$ cat Macros.scala
import language.experimental.macros
import scala.reflect.macros.Context
trait Iso[T, U] {
def to(t : T) : U
// def from(u : U) : T
}
object Iso {
implicit def materializeIso[T, U]: Iso[T, U] = macro impl[T, U]
def impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context): c.Expr[Iso[T, U]] = ...
}
22:06 ~/Projects/Kepler_5923/sandbox (ticket/5923)$ scala
Welcome to Scala version 2.10.2-20130430-211936-b00a62fd16 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def foo[C, L](c: C)(implicit iso: Iso[C, L]) = println(iso.to(c))
foo: [C, L](c: C)(implicit iso: Iso[C,L])Unit
scala> case class Foo(i: Int, s: String, b: Boolean)
defined class Foo
scala> foo(Foo(23, "foo", true))
(23,foo,true)
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment