Skip to content

Instantly share code, notes, and snippets.

@edofic
Created June 26, 2013 17:31
Show Gist options
  • Save edofic/5869487 to your computer and use it in GitHub Desktop.
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
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>
*/
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