Scala macro to generate new instance of class with dependency injection of trait with abstract fields -
update: modified code use macro annotation, still missing something
the following code works (question below):
object mytypes { trait realtype class container[rt <: realtype] { this: rt => } trait somerealtype extends realtype trait partialrealtype extends realtype { val needed: int } } import mytypes._ object container { import scala.reflect.macros.blackbox.context import scala.language.experimental.macros def helper[rt <: realtype : c.weaktypetag](c : context) : c.expr[rt] = { import c.universe._ val weakrt = weaktypeof[rt] val gentree = q"new container[$weakrt] $weakrt {}" c.expr(gentree) } def apply[rt <: realtype] : container[rt] rt = macro helper[rt] } val ok = new container[somerealtype] somerealtype val ok_quasi = container[somerealtype]
i wish able run following:
val partial = container[partialrealtype]{val needed: int = 0}
i've tried this:
object container { import scala.reflect.macros.blackbox.context import scala.language.experimental.macros def helper[rt <: realtype : c.weaktypetag](c : context)(body: c.expr[unit]) : c.expr[rt] = { import c.universe._ val weakrt = weaktypeof[rt] val gentree = q"new container[$weakrt] $weakrt { $body }" c.expr(gentree) } def apply[rt <: realtype](body: unit) : container[rt] rt = macro helper[rt] }
however doesn't work.
Comments
Post a Comment