Created
July 3, 2012 12:29
-
-
Save larsrh/3039449 to your computer and use it in GitHub Desktop.
Attempting a by-name implicitly
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, I see. Nice!