如何在 Scala 用户定义的注释中使用命名参数? [英] How to use named arguments in Scala user defined annotations?

查看:29
本文介绍了如何在 Scala 用户定义的注释中使用命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我能够获得第一个注释对象Publishable",但不能获得第二个.第二个使用命名参数,转换为 x$2、x$3、x$1 作为 AST 中的参数.我该如何正确执行此操作?

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
    })
})

输出:

==== 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)
....

推荐答案

编译您的代码会产生警告:

Compilation of your code produces a warning:

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
  @Publishable(saveMode = "c")

您可以使用默认参数,如

You can work with default parameters like in

tree = new Publishable("a", "b", $line41.$read.$iw.$iw.Publishable.<init>$default$3)

使用这种技术:我如何访问默认值参数值通过 Scala 反射? 用于名为 $lessinit$greater$default$3 的方法.

using this technique: How do I access default parameter values via Scala reflection? for methods named like $lessinit$greater$default$3.

但是关于像在

tree = new Publishable(x$2, x$3, x$1)

它们的值存储在局部变量中(错误说Could not find proxy for val x$2)并且不能通过反射获得局部变量(Scala 反射和 Java 反射都没有)https://www.thecodingforums.com/threads/java-reflection-with-local-variables.599017/

their values are stored in local variables (the error says Could not find proxy for val x$2) and local variables can't be obtained via reflection (neither Scala reflection nor Java reflection) https://www.thecodingforums.com/threads/java-reflection-with-local-variables.599017/

您可以在编译时使用注释的默认和命名参数:从 Scala 宏注释中获取参数

You can work with default and named parameters of macro annotation at compile time: Getting Parameters from Scala Macro Annotation

这篇关于如何在 Scala 用户定义的注释中使用命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆