Welcome to Scala version 2.10.0-RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.reflect.runtime.{universe=>ru}
import scala.reflect.runtime.{universe=>ru}
scala> def f(x: Int) = 0
f: (x: Int)Int
scala> val g = ru.reify(f _)
g: reflect.runtime.universe.Expr[Int => Int] =
Expr[Int => Int]({
((x) => f(x))
})
scala> ru.reify(g.splice _)
res0: reflect.runtime.universe.Expr[() => Int => Int] =
Expr[() => Int => Int]((() => {
((x) => f(x))
}))
scala> :pas
// Entering paste mode (ctrl-D to finish)
object A {
def f(x: Int, s: String) = 0
}
def getf[T] = macro getfImpl[T]
def getfImpl[T: c.WeakTypeTag](c: Context) = {
import c.universe._
c.Expr(treeBuild.mkAttributedIdent(weakTypeOf[T].member(newTermName("f"))))
}
scala> def g = getf[A.type]
<console>:12: error: missing arguments for method f in object A;
follow this method with `_' if you want to treat it as a partially applied function
def g = getf[A.type]
C:\Users\Dave>scala
Welcome to Scala version 2.10.0-RC2 (Oracle JRockit(R), Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import language.experimental.macros
import language.experimental.macros
scala> import scala.reflect.macros.Context
import scala.reflect.macros.Context
scala> :paste
// Entering paste mode (ctrl-D to finish)
object A {
def f(x: Int, s: String) = 0
}
def getf[T]: Any = macro getfImpl[T]
def getfImpl[T: c.WeakTypeTag](c: Context) = {
import c.universe._
val fSym = weakTypeOf[T].member(newTermName("f")).asMethod
val argSyms = fSym.paramss.flatten
val funTree = Function(argSyms.map(ValDef(_)), treeBuild.mkMethodCall(fSym, argSyms.map(s =>
treeBuild.mkAttributedIdent(s)
)))
c.Expr(funTree)
}
// Exiting paste mode, now interpreting.
defined module A
getf: [T]=> Any
getfImpl: [T](c: scala.reflect.macros.Context)(implicit evidence$1: c.WeakTypeTa
g[T])c.Expr[T]
scala> def g = getf[A.type]
g: (Int, String) => Int