scala-quasiquotes

Type mismatch in scala quasiquote of macro definition: “type mismatch; found : field.NameType required: c.universe.TermName”

烂漫一生 提交于 2020-01-15 12:18:06
问题 I asked a longer question, but it seems it's too much code for people to sort through so I've created this question to focus on one smaller, specific problem I'm facing regarding use of macros in Scala. Consider the following code snippet: 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 = fields.map { field => val name = field.name val

Performance and caching of generated code in Scala

 ̄綄美尐妖づ 提交于 2020-01-04 05:14:28
问题 I need to generate an implementation of a trait at runtime, then execute a known method on an instance of the trait. In this example I'm running A 's a method: import reflect.runtime._, universe._, tools.reflect.ToolBox package p { trait A { def a: String } val tb = currentMirror.mkToolBox() val d: A = tb.eval(q"""class C extends p.A { def a = "foo" }; new C""").asInstanceOf[A] println(d.a) // "foo" } A few questions around this: The use case is very performance-sensitive (running generated

“macro implementation reference has wrong shape” in the Scala Documentation examples

孤者浪人 提交于 2020-01-03 16:48:25
问题 The following macro is pasted from http://docs.scala-lang.org/overviews/quasiquotes/usecases.html: import reflect.macros.Context import language.experimental.macros val universe = reflect.runtime.universe; import universe._ import reflect.runtime.currentMirror import tools.reflect.ToolBox val toolbox = currentMirror.mkToolBox() object debug { def apply[T](x: =>T): T = macro impl def impl(c: Context)(x: c.Tree) = { import c.universe._ val q"..$stats" = x val loggedStats = stats.flatMap { stat

“macro implementation reference has wrong shape” in the Scala Documentation examples

独自空忆成欢 提交于 2020-01-03 16:43:33
问题 The following macro is pasted from http://docs.scala-lang.org/overviews/quasiquotes/usecases.html: import reflect.macros.Context import language.experimental.macros val universe = reflect.runtime.universe; import universe._ import reflect.runtime.currentMirror import tools.reflect.ToolBox val toolbox = currentMirror.mkToolBox() object debug { def apply[T](x: =>T): T = macro impl def impl(c: Context)(x: c.Tree) = { import c.universe._ val q"..$stats" = x val loggedStats = stats.flatMap { stat

How to get the runtime value of parameter passed to a Scala macro?

点点圈 提交于 2019-12-23 10:19:10
问题 I have an ostensibly simple macro problem that I’ve been banging my head against for a few hours, with no luck. Perhaps someone with more experience can help. I have the following macro: import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context object MacroObject { def run(s: String): Unit = macro runImpl def runImpl(c: Context)(s: c.Tree): c.Tree = { import c.universe._ println(s) // <-- I need the macro to know the value of s at compile time q"()" } } The

How does one obtain the type of the value that an AST represents?

痴心易碎 提交于 2019-12-13 06:16:08
问题 I’m trying to write the following: import scala.reflect.runtime.universe._ val value: Tree = /* some AST */ val tpe = typeOf(value) // This should be the result type of the AST. // This is pseudocode. What should // actually go on this line? q""" type U = $tpe val v: U = $value """ I need to capture the type of the value represented by the AST value in tpe and assign it to U . How does one do this? Edit: Giving a type annotation for value and matching on it via quasiquotes isn't an option

Conflicting cross-version suffixes in: org.scalamacros:quasiquotes

有些话、适合烂在心里 提交于 2019-12-12 14:24:48
问题 I am trying to use scala-pickling in one of my projects. I tried to mimic the build file of macroid which seems to use pickling too but I keep getting this error on sbt test : [error] Modules were resolved with conflicting cross-version suffixes in dijon: [error] org.scalamacros:quasiquotes _2.10, _2.10.3 java.lang.RuntimeException: Conflicting cross-version suffixes in: org.scalamacros:quasiquotes at scala.sys.package$.error(package.scala:27) at sbt.ConflictWarning$.processCrossVersioned

Quasiquotes for multiple parameters and parameter lists

心已入冬 提交于 2019-12-12 08:38:44
问题 Quasiquotes are amazing—they make writing macros in Scala hugely less painful, and in my experience they almost always just work exactly as I'd expect. And best of all, they're now available as a plugin in Scala 2.10. This question is about a small problem that I ran into while writing this blog post. It's on my list of stuff to look into when I can find a few minutes, but I figured I'd post it here in case someone else can beat me to it, and to help other people who are running into the same