scala-macros

Where can I learn about constructing AST's for Scala macros?

扶醉桌前 提交于 2019-12-17 17:24:53
问题 Where I can learn how to construct the AST's that Scala's macros generate? The Scaladoc isn't as helpful as I'd like. For example: abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): Universe.Tree A factory method for Apply nodes. But how do I figure out what an Apply node is? Where can I find a list of the node types in AST's, and how they fit together? 回答1: There isn't a lot of documentation for the internals of the compiler available, but the things that are available should be

Scala Macros: Making a Map out of fields of a class in Scala

只谈情不闲聊 提交于 2019-12-17 07:07:52
问题 Let's say that I have a lot of similar data classes. Here's an example class User which is defined as follows: case class User (name: String, age: Int, posts: List[String]) { val numPosts: Int = posts.length ... def foo = "bar" ... } I am interested in automatically creating a method ( at compile time ) that returns a Map in a way that each field name is mapped to its value when it is called in runtime. For the example above, let's say that my method is called toMap : val myUser = User("Foo",

Why does typecheck return NoType, even when it's calculated a valid symbol?

我的梦境 提交于 2019-12-14 02:24:27
问题 Following on from: How to Typecheck a DefDef First, some snippets from my macro: object log { def err(msg: String): Unit = c.error(c.enclosingPosition, msg) def warn(msg: String): Unit = c.warning(c.enclosingPosition, msg) def info(msg: String): Unit = c.info(c.enclosingPosition, msg, force=true) def rawInfo(name: String, obj: Any): Unit = info(name + " = " + showRaw(obj)) } methodsIn(body) foreach { dd => //dd: DefDef val name = dd.name.toString log.rawInfo(name, dd) log.rawInfo(name + ".rhs

Handling by-name parameters in Scala macro

余生颓废 提交于 2019-12-13 17:06:33
问题 I have a macro that does some analysis on nested function applications. It matches applications and retrieve the parameter types this way: case q"$f[..$targs](..$args)(...$otherArgs)" => // retrieve the list of all parameter types val paramTpes = f.tpe match { case pmt: PolyType if pmt.paramLists.size == 0 => Seq() case pmt: PolyType => pmt.paramLists(0) map {_.typeSignature .substituteTypes(pmt.typeParams, targs map (_.tpe))} case pmt: MethodType if pmt.paramLists.size == 0 => Seq() case pmt

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

Scala: macro to create an instance from a class body

安稳与你 提交于 2019-12-13 04:18:55
问题 I am building a DSL in Scala, and for that, I need to store "instances" of a class ( Parent in this case), except these "instances" must be re-created several times at runtime. So instead I am storing "constructor functions" - a lambda that makes the instance. consider the following code - imagine that userVar can change at runtime and the updated value must be used when constructing the instances. class Parent { def func(param: Any): Unit = { ... } } class User { def construct(constr: =>

automatically generate case object for case class

感情迁移 提交于 2019-12-13 03:16:39
问题 How can I have the scala compiler automatically generate the case object? // Pizza class class Pizza (val crust_type: String) // companion object object Pizza { val crustType = "crust_type" } Desired properties for case object for each attribute in the case class generate an attribute in the case object set the value in of each corresponding case object to the string representation of the attribute name and change camelCase to snake_case for the object attribute name, keep snake_case for

Compilation failure is confirmed at the moment of accessing typeSignature of a specific symbol at macro expansion

倾然丶 夕夏残阳落幕 提交于 2019-12-13 02:57:04
问题 I had to look at specific declarations in the macro and examine them individually. Many such errors occurred when trying to examine the akka stream declaration. def getType(symbol: Symbol): Type = { symbol.typeSignature } [error] error while loading SmallSortedMap$Entry, class file '/Users/xxx/.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken [error] (class java.util.NoSuchElementException/key not found: K)

Scala macro modify object

爷,独闯天下 提交于 2019-12-13 02:06:15
问题 I have a next macro annotation class Foo(obj: String) extends StaticAnnotation { def macroTransform(annottees: Any*) = macro MacroImpl.impl } object MacroImpl { def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { import c.universe._ // i want find `obj` and modify body } } // usage @Foo("pkg.myObject") class SomeClass {} Is it possible with macro find object by name and modify body of object? 回答1: This is currently impossible, because macros in Scala can't modify anything outside

“too few argument lists for macro invocation”

元气小坏坏 提交于 2019-12-13 01:27:45
问题 Given the following code: case class JetDim(dimension: Int) { require(dimension > 0) } object JetDim { def build(dimension: Int): Int = macro JetDimMacro.apply } and the macro that it calls: def apply(dimension: Int): Int = macro applyImpl def applyImpl(c: Context)(dimension: c.Expr[Int]): c.Expr[Int] = ... I'm getting this compile-time error: [error] too few argument lists for macro invocation [error] def build(dimension: Int): Int = macro JetDimMacro.apply Why? 回答1: The macro keyword takes