Created
June 26, 2013 17:31
-
-
Save edofic/5869487 to your computer and use it in GitHub Desktop.
lying about return type (scala macros). compiler infers the type of generated code. sweet. as a bonus you can crash the compiler if you mix in some singleton types
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 reflect.macros.Context | |
import language.experimental.macros | |
def producerImpl[A: c.WeakTypeTag](c: Context): c.Expr[Any] = { | |
c.universe.reify{ | |
() => (??? : A) | |
} | |
} | |
def producer[A] = macro producerImpl[A] | |
/* | |
scala> producer[Int] | |
res0: () => Int = <function0> | |
*/ |
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 reflect.macros.Context | |
import language.experimental.macros | |
class Box[+A] | |
object Box { | |
def apply[A <: AnyRef](a: A) = new Box[a.type] | |
} | |
object Main { | |
def iffImpl(c: Context): c.Expr[Any] = { | |
import c.universe._ | |
val a = c.literal("a") | |
reify{ Box("a") } | |
} | |
def iff = macro iffImpl | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment