scala-macros

Macro annotation to override toString of Scala function

假如想象 提交于 2020-07-04 01:54:33
问题 How to write macro annotation which looks in usage like @named("+2") _ + 2 and produces: new (Int => Int) { override def toString(): String = "+2" def apply(x: Int): Int = x + 2 } 回答1: Correct syntax is ((_: Int) + 2): @named("+2") . Unfortunately macro annotations annotating expressions don't expand. The simplest is to use object Named { def build[T, R](name: String)(applyFunc: T => R): T => R = new (T => R) { override def toString() = name def apply(x: T): R = applyFunc(x) } } without any

Getting Parameters from Scala Macro Annotation

梦想与她 提交于 2020-01-19 12:42:30
问题 So I have an annotation on a function (DefDef). This annotation has parameters. However, I am confused on how to get the parameters from the constructor. Usage example: class TestMacro { @Foo(true) def foo(): String = "" foo } Here's the code for the annotation: class Foo(b: Boolean) extends StaticAnnotation { def macroTransform(annottees: Any*) = macro Foo.impl } object Foo { def impl(c: whitebox.Context)(annottees: c.Tree*): c.Expr[Any] = { import c.universe._ //how do I get value of `b`

How to get a raw sequence of reified values in scala macro

白昼怎懂夜的黑 提交于 2020-01-17 05:22:34
问题 I have a c.Expr[Seq[T]] and I want to get a Seq[c.Expr[T]] so I can pass it to a library function that processes it and produces a different final result Expr. I found this answer explaining how to go in the opposite direction but I can't quite figure out how to invert the process. reify { xs.splice.map(x => reify { x }) } gives me c.Expr[Seq[c.Expr[T]]] , which isn't quite right. 来源: https://stackoverflow.com/questions/39761482/how-to-get-a-raw-sequence-of-reified-values-in-scala-macro

Scala macros for nested case classes to Map and other way around

℡╲_俬逩灬. 提交于 2020-01-16 12:23:13
问题 I want to convert any case class to a Map[String,Any] for example: case class Person(name:String, address:Address) case class Address(street:String, zip:Int) val p = Person("Tom", Address("Jefferson st", 10000)) val mp = p.asMap //Map("name" -> "Tom", "address" -> Map("street" -> "Jefferson st", "zip" -> 10000)) val p1 = mp.asCC[Person] assert(p1 === p) Possible duplications: Here is a question that with reflection answer. Here is a question for (converting from case class to map (without

Scala macros for nested case classes to Map and other way around

狂风中的少年 提交于 2020-01-16 12:21:10
问题 I want to convert any case class to a Map[String,Any] for example: case class Person(name:String, address:Address) case class Address(street:String, zip:Int) val p = Person("Tom", Address("Jefferson st", 10000)) val mp = p.asMap //Map("name" -> "Tom", "address" -> Map("street" -> "Jefferson st", "zip" -> 10000)) val p1 = mp.asCC[Person] assert(p1 === p) Possible duplications: Here is a question that with reflection answer. Here is a question for (converting from case class to map (without

Generate companion object for case class with methods (field = method)

二次信任 提交于 2020-01-15 11:46:06
问题 Generate companion object for case class with scala-macros some code example that i tried, it works i can get list of tuple (name -> type) but how to generate the object in the same scope? import c.universe._ val tpe = weakTypeOf[T] val fields = tpe.decls.collectFirst { case m: MethodSymbol if m.isPrimaryConstructor => m } .get .paramLists .head val extractParams = fields.map { field => val name = field.asTerm.name val fieldName = name.decodedName.toString val NullaryMethodType(fieldType) =

How do I resolve a short type name to a full type name in a Macro

馋奶兔 提交于 2020-01-14 01:57:59
问题 Inside a macro is there a way of using the current Context to fully expand a type name? Eg something like: context.resolveShortTypeNameToFullTypeName("Foo") = "com.acme.Foo" 回答1: Your macro might expand in a tree that includes an arbitrary import prefix.Foo , so you're asking if you can query that enclosing tree: If I emit a name Foo , how would you typecheck it? symbol.fullName is your answer. val t = c.typeCheck(q"??? : Foo").tpe.typeSymbol.fullName or use c.typecheck in 2.11. or, if you

Macro to access source code of function at runtime

核能气质少年 提交于 2020-01-13 11:35:29
问题 Using Scala macros I would like to get access to source code of function f. Here is simplified example of my problem: def logFImplementation(f: => Boolean) { val sourceCodeOfF: String = ... // <-- how to get source code of f?? println("Scala source of f=" + sourceCodeOfF) } so for: logFImplementation { val i = 5; List(1, 2, 3); true } it should print: Scala source of f=val i: Int = 5; immutable.this.List.apply[Int](1, 2, 3); true (Right now I tested Macro to access source code text at runtime

Scala macro can't find my java class

家住魔仙堡 提交于 2020-01-05 03:54:13
问题 I'm creating a scala macro to automatically generate case classes from POJOs (in order to make working with avro a little bit nicer). Everything "works" except that the compiler - during macro expansion only - chokes on my java class. If I comment out the call to the macro, everything compiles fine. My question is: how do I generate code in the macro such that the compiler resolves my own java classes? Example error message: [info] Compiling 1 Scala source to /Users/marcin/development/repos

What is wrong with this def macro?

被刻印的时光 ゝ 提交于 2020-01-04 13:04:08
问题 Disclaimer: this code is not of practical use and serves education purposes only. tl;dr: most of it is just a result of me trying to debug the issue, so only first 3 snippets matter. Here is the macro definition: def tx[T](ds: GraphDatabaseService)(block: => T): Option[T] = macro txmacros.blockTxImpl[T] Here is the implementation: def blockTxImpl[T: c.WeakTypeTag](c: whitebox.Context)(ds: c.Tree)(block: c.Tree): c.Tree = { import c.universe._ q""" val tx = $ds.beginTx() val newRetVal = try {