scala-macros

How to make IntelliJ IDEA recognise code created by macros?

孤者浪人 提交于 2019-12-22 05:35:24
问题 Background I have an sbt-managed Scala project that uses the usual sbt project layout for Scala projects with macros, i.e., a subproject that contains the macros a main project that is the actual application and that depends on the macro subproject. The macros are macro annotations which, in essence, generate companion objects for regular classes. The generated companion objects declare, amongst other members, apply/unapply methods. I used the sbt-idea plugin to generate a corresponding

how to print variable name and value using a scala macro?

别来无恙 提交于 2019-12-22 05:19:25
问题 I am sure there is a more elegant way of writing the following macro which prints the name and value of a variable: def mprintx(c: Context)(linecode: c.Expr[Any]): c.Expr[Unit] = { import c.universe._ val namez = (c.enclosingImpl match { case ClassDef(mods, name, tparams, impl) => c.universe.reify(c.literal(name.toString).splice) case ModuleDef(mods, name, impl) => c.universe.reify(c.literal(name.toString).splice) case _ => c.abort(c.enclosingPosition, "NoEnclosingClass") }).toString match {

How can parameters/settings be passed to a Scala macro?

耗尽温柔 提交于 2019-12-22 03:43:15
问题 How can parameters/settings be passed to a Scala macro? These settings should not be global, but per macro invocation. What I would like to have is something similar to this: def a(param: Int) = macro internalMacro("setting 1") def b(param: Int) = macro internalMacro("setting 2") whereas setting 1 and setting 2 should then be constant values, accessible from within the macro, so I can make the internal behavior dependent on them. 回答1: The parameter lists of your method and the macro

How to build a dynamic sequence in a scala macro?

烂漫一生 提交于 2019-12-21 17:53:13
问题 I have a scala macro which outputs nested case classes. I can assemble fragments of expressions created using reify to build up the nested case classes programmatically: case class Foo(name: String) case class Bar(foo: Foo) def foo(name: String) = { c.universe reify { Foo(c.literal(name).splice) } } def bar(foo: Expr[Foo]) = { c.universe reify { Bar(foo.splice) } } // output Bar(Foo("MyFoo")) c.Expr( bar(foo("MyFoo").asInstanceOf[Expr[Foo]]).tree ) Things work well apart from the annoying

Instantiate a class with Scala Macro or reflection

两盒软妹~` 提交于 2019-12-21 12:29:42
问题 On my scala code, I want to be able to instantiate a new class. For instance, supose I have the code below: class Foo { def foo=10 } trait Bar { val bar=20 } Ideally, I want to be able to do something like: def newInstance[A <: Foo] = { new A with Bar } newInstance[Foo] But, of course this doesn't work. I tried to use reflection to instantiate a class, but it seems that I'm only able to instantiate a new class (and not mix-in with a trait). I think it would be possible to make this work using

Creating a method definition tree from a method symbol and a body

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 09:45:42
问题 Is there a convenient way to turn a MethodSymbol into the left-hand side of a method definition tree (i.e., a DefDef) in Scala 2.10? For example, suppose I want to create a macro that will take an instance of a trait and wrap all of that trait's methods with some debugging functionality. I can write the following: import scala.language.experimental.macros import scala.reflect.macros.Context object WrapperExample { def wrap[A](a: A): A = macro wrap_impl[A] def wrap_impl[A: c.WeakTypeTag](c:

Using LabelDef in scala macros (2.10)

纵饮孤独 提交于 2019-12-21 04:59:21
问题 I'm experimenting with the scala 2.10 macro features. I have trouble using LabelDef in some cases, though. To some extent I peeked in the compiler's code, read excerpts of Miguel Garcia's papers but I'm still stuck. If my understanding is correct , a pseudo-definition would be: LabelDef(labelName, listOfParameters, stmsAndApply) where the 3 arguments are Trees and: - labelName is the identifier of the label $L being defined - listOfParameters correspond to the arguments passed when label-

Add a compile time only sub-project dependency in sbt

怎甘沉沦 提交于 2019-12-21 02:34:08
问题 I have a multi-project contains a private macro sub-project which's usage is limited to implement method body of other sub-projects. Neither should it be on the runtime classpath of other sub-projects, nor should it be visible in any form in the published POM of other sub-projects. So that other sbt project could use library from this project without knowing the macro sub-project. For external dependency, I found this SO Q&A works perfectly, but for sub-project when I trying to do the similar

Scala macro to print code?

孤者浪人 提交于 2019-12-18 05:01:49
问题 I want to do something like this: def assuming[A](condition: => Boolean)(f: => A): A = { require(condition, /* print source-code of condition */) f } Sample usage: def fib(n: Int) = n match { // yes, yes, I know this is not efficient case 0 => 0 case 1 => 1 case i => assuming(i > 0) { fib(i-1) + fib(i-2) } } Now, for example, if you call fib(-20) , I want it to throw an exception with a message like Assertion failed: -20 > 0 or Assertation failed: i > 0 回答1: Dude, isn't an assert macro one of

Scala macro to print code?

不想你离开。 提交于 2019-12-18 05:01:06
问题 I want to do something like this: def assuming[A](condition: => Boolean)(f: => A): A = { require(condition, /* print source-code of condition */) f } Sample usage: def fib(n: Int) = n match { // yes, yes, I know this is not efficient case 0 => 0 case 1 => 1 case i => assuming(i > 0) { fib(i-1) + fib(i-2) } } Now, for example, if you call fib(-20) , I want it to throw an exception with a message like Assertion failed: -20 > 0 or Assertation failed: i > 0 回答1: Dude, isn't an assert macro one of