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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -