scala - Compile errors when defining a macro to convert a case class instance into a map and back -
i'm trying understand following blog post, discusses how use macros create generic macro-based approach convert case class objects , map: http://blog.echo.sh/post/65955606729/exploring-scala-macros-map-to-case-class-conversion
even though read entire post , article on scala macros, still not understand how work. unfortunately, i'm receiving couple of compile errors example code given in blog post , not sure why errors occurring nor how resolve them. i'm reproducing code below, preceded compile errors, completeness (i annotated code snippet comments regarding compile errors). know why these compiler issues occurring , how resolve them?
-type mismatch; found : field.nametype required: c.universe.termname -can't unquote list[nothing] .., bottom type values indicate programmer mistake
here's code:
import scala.reflect.macros.context import scala.language.experimental.macros trait mappable[t] { def tomap(t: t): map[string, any] def frommap(map: map[string, any]): t } object mappable { implicit def materializemappable[t]: mappable[t] = macro materializemappableimpl[t] def materializemappableimpl[t: c.weaktypetag](c: context): c.expr[mappable[t]] = { import c.universe._ val tpe = weaktypeof[t] val companion = tpe.typesymbol.companionsymbol val fields = tpe.declarations.collectfirst { case m: methodsymbol if m.isprimaryconstructor ⇒ m }.get.paramss.head val (tomapparams, frommapparams) = fields.map { field ⇒ val name = field.name val decoded = name.decoded val returntype = tpe.declaration(name).typesignature (q"$decoded → t.$name", q"map($decoded).asinstanceof[$returntype]") // error 1 }.unzip c.expr[mappable[t]] { q""" new mappable[$tpe] { def tomap(t: $tpe): map[string, any] = map(..$tomapparams) def frommap(map: map[string, any]): $tpe = $companion(..$frommapparams) // error 2 } """ } } }
change t.$name t.${name.totermname}
Comments
Post a Comment