Macro-powered structural types
import scala.annotation.StaticAnnotation | |
import scala.reflect.macros.Macro | |
import language.experimental.macros | |
class body(tree: Any) extends StaticAnnotation | |
trait Macros extends Macro { | |
import c.universe._ | |
def selFieldImpl = { | |
val field = c.macroApplication.symbol | |
val bodyAnn = field.annotations.filter(_.tpe <:< typeOf[body]).head | |
bodyAnn.scalaArgs.head | |
} | |
def mkObjectImpl(xs: c.Tree*) = { | |
val kvps = xs.toList map { case q"${_}(${Literal(Constant(name: String))}).->[${_}]($value)" => name -> value } | |
val fields = kvps map { case (k, v) => q"@body($v) def ${TermName(k)} = macro Macros.selFieldImpl" } | |
q"class Workaround { ..$fields }; new Workaround{}" | |
} | |
} | |
object mkObject { | |
def apply(xs: Any*) = macro Macros.mkObjectImpl | |
} | |
================= | |
object Test { | |
def main(args: Array[String]) = { | |
val foo = mkObject("x" -> "2", "y" -> 3) | |
println(foo.x) | |
println(foo.y) | |
// println(foo.z) => will result in a compilation error | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
I've re-implemented it for a newer version of Scala, but when I'm trying to use these types - I see
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
No reflection involved!