Skip to content

Instantly share code, notes, and snippets.

@larsrh
Created July 3, 2012 12:29
Show Gist options
  • Save larsrh/3039449 to your computer and use it in GitHub Desktop.
Save larsrh/3039449 to your computer and use it in GitHub Desktop.
Attempting a by-name implicitly
import language.experimental.macros
import scala.reflect.makro.Context
object Name {
def nameplicitly[T](implicit t0: T): Name[T] = macro nameplicitly_impl[T]
def nameplicitly_impl[T : c.TypeTag](c: Context)(t0: c.Expr[T]): c.Expr[Name[T]] =
c.reify(new Name[T] { def t = t0.splice })
}
trait Name[T] {
def t: T
}
// now to test!
Welcome to Scala version 2.10.0-20120702-060901-33936243bd (OpenJDK 64-Bit Server VM, Java 1.7.0_05-icedtea).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class Foo
defined class Foo
scala> implicit def foo = { println("o hai"); new Foo }
foo: Foo
scala> Name.nameplicitly[Foo]
res0: Name[Foo] = Name$$anon$1@54009218
scala> res0.t
o hai
res1: Foo = Foo@d64ea8b
scala> res0.t
o hai
res2: Foo = Foo@308f8cd8
@hseeberger
Copy link

Ah, I see. Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment