How to use named arguments in Scala user defined annotations?


#1

In the following code, I was able get the first annotation object “Publishable”, but not the second. The 2nd one used named arguments, which translated to x$2, x$3, x$1 as arguments in the AST. How do I do this properly?

class Publishable(
    val path: String = "",
    val format: String = "",
    val saveMode: String = "overwrite"
) extends scala.annotation.StaticAnnotation {
    def show: Unit = {
        println(s"path=$path, format=$format, saveMode=$saveMode")
    }
}

class TestObject {
    @Publishable("a", "b")
    def method1 = 100

    @Publishable(saveMode = "c")
    def method2 = 200
}

import scala.reflect.runtime.{universe => ru}
import ru._

val mirror = runtimeMirror(getClass.getClassLoader)
typeOf[TestObject].decls.foreach(field => {
    println(s"==== $field ====")
    field.annotations.foreach(anno => {
        println(s"tree = ${show(anno.tree)}")
        import scala.tools.reflect.ToolBox
        val tb = mirror.mkToolBox()
        val pub = tb.eval(tb.untypecheck(anno.tree)).asInstanceOf[Publishable]
        pub.show
    })
})

Output:

==== constructor TestObject ====
==== method method1 ====
tree = new Publishable("a", "b", $line41.$read.$iw.$iw.Publishable.<init>$default$3)
path=a, format=b, saveMode=overwrite
==== method method2 ====
tree = new Publishable(x$2, x$3, x$1)
java.lang.IllegalArgumentException: Could not find proxy for val x$2: String in List(value x$2, value <local TestObject>, class TestObject, object $iw, object $iw, object $read, package $line42, package <root>) (currentOwner= method wrapper )
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:326)
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:331)
....

#2

This is a bit of an unfortunate interaction with the implementation of named/default arguments, on which the implementation of annotations builds.

First an example with a normal method (not an annotation):

$> scala -Xprint:typer
scala> def f(a: Int = 1, b: Int = 2) = 0
...
scala> f(b = 3)
...
        private[this] val res0: Int = {
          <artifact> val x$1: Int = 3;
          <artifact> val x$2: Int = $line6.$read.$iw.$iw.f$default$1;
          $line6.$read.$iw.$iw.f(x$2, x$1)
        };
...

The invocation f(b = 3) is translated into a block with two local variables. The reason is to ensure evaluation order, i.e. the explicit argument 3 is evaluated before the default argument expression f$default$1.

The same thing happens with annotations. Unfortunately, the result is that in the actual instantiation of the annotation, the expression passed to the constructor is a reference to the local variable. In fact, the compiler prints a (rather cryptic) warning about it. Maybe this should be an error…

$> scala -Xprint:typer
scala> class A(a: Int = 1, b: Int = 2) extends annotation.StaticAnnotation
...
scala> @A(b = 3) def f = 0
<console>:13: warning: Usage of named or default arguments transformed this annotation
constructor call into a block. The corresponding AnnotationInfo
will contain references to local values and default getters instead
of the actual argument trees
       @A(b = 3) def f = 0
        ^
...
        @A(x$2, x$1) def f: Int = 0
...