scala-macros

Retrieve the name of the value a Scala macro invocation will be assigned to

一世执手 提交于 2019-12-24 00:21:26
问题 I'm attempting to write a macro that would wrap a function and deducting a parameter from the value its invocation will be assigned to. object TestMacros { def foo(name: String): String = name.toUpper def bar = macro barImpl def barImpl(c: Context): c.Expr[String] = { import c.universe._ //TODO extract value name (should be baz) c.Expr[String](Apply( Select(newTermName("TestMacros"), newTermName("foo")), // Probably wrong, just typed it quickly for demonstration purposes List(Literal(Constant

Access code file and line number from Scala macro?

让人想犯罪 __ 提交于 2019-12-23 17:20:57
问题 How can I access the name of the code file and line number in a Scala macro? I looked at SIP-19 and it says it can be easily implemented using macros... EDIT: To clarify, I want the code file and line number of the caller. I already have a debug macro and I want to modify it to print the line number and file name of whoever calls debug 回答1: You want c.macroApplication.pos , where c is for Context . c.enclosingPosition finds the nearest macro on the stack that has a position. (See the other

simple scala macro

℡╲_俬逩灬. 提交于 2019-12-23 17:19:58
问题 I would like to have a scala macro that does the following: When I write: myCreateCityMacro("paris") myCreateCityMacro("vallorbe") I would like to get: val paris = new City("paris") val vallorbe = new City("vallorbe") 回答1: This can be solved using scala Dynamic feature: import scala.language.dynamics object Cities extends App { var c = new DynamicMap[String, City]() createCity("Paris") createCity("Vallorbe") println(c.Paris, c.Vallorbe) def createCity(name: String) { c.self.update(name, new

What does it mean when macro annotation cannot be used in the same compilation that defines it?

你离开我真会死。 提交于 2019-12-23 12:38:19
问题 I am curious about this statement: Error:(3, 18) ...another possibility is that you try to use macro annotation in the same compilation run that defines it) I tried googling and found this: Finally, remember that using macros requires compilation to happen in two steps: first, compile the macros, then compile the code where the macros are used. This is necessary so that your macros can be run before the rest of your code is compiled. For example if you use SBT, you can configure Build.scala

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

Generic Macros with Quill

本小妞迷上赌 提交于 2019-12-23 04:54:36
问题 Hi so I've been trying to create some generic functions using macros and Quill. Here is my implementation of the macro: class Impl(val c: Context) { import c.universe._ def all[T](tblName: Tree, ctx: Tree)(implicit t: WeakTypeTag[T]): Tree = q""" import ${ctx}._ implicit val schema = schemaMeta[$t](${tblName}) run(quote { query[$t] }) """ } object Macros { def all[T](tblName: String, ctx: MysqlAsyncContext[Literal.type]): Future[List[T]] = macro Impl.all[T] } And I've tried using it in the

Provide implicits for all subtypes of sealed type

老子叫甜甜 提交于 2019-12-23 03:41:21
问题 In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type

Provide implicits for all subtypes of sealed type

蓝咒 提交于 2019-12-23 03:41:08
问题 In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type

Get fully qualified method name in scala macros

故事扮演 提交于 2019-12-23 01:24:09
问题 I use Scala macros and match Apply and I would like to get fully qualified name of the function which is called. Examples: println("") -> scala.Predef.println scala.Predef.println("") -> scala.Predef.println class Abc { def met(): Unit = ??? } case class X { def met(): Unit = ??? def abc(): Abc = ??? } val a = new Abc() val x = new Abc() a.met() -> Abc.met new Abc().met() -> Abc.met X() -> X.apply X().met() -> X.met x.met() -> X.met x.abc.met() -> Abc.met On the left side is what I have in

Is there any workaround for Scala bug SI-7914 - returning an apply method from a Scala macro?

陌路散爱 提交于 2019-12-22 10:22:48
问题 In our library we have a macro defined like so: def extractNamed[A]: Any = macro NamedExtractSyntax.extractNamedImpl[A] This macro uses its type signature to return an apply method whose arguments match the apply method of the case class on which it is typed. The idea is that this allows the user of our library to make use of Scala's named parameters like so: lazy val fruitExtractor = extractNamed[Fruit]( name = FruitTable.name, juiciness = FruitTable.juiciness ) However, this doesn't seem to