我正在尝试在注释中添加常量和文字,但最终得到“注释参数需要是常量”错误。我设置了一些示例代码也会产生相同的错误。
package test
@TestAnnotation(Constants.test + ", world!")
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
package test
object Constants {
final val test = "Hello"
}
package test;
@interface TestAnnotation {
String value();
}
答案 0 :(得分:0)
我正在查看http://www.scala-lang.org/old/node/9363并希望告诉您static
上需要final
和/或Constants.test
(您已经拥有),但我跑了你的代码在<2.10.4
$ sbt run
[info] Compiling 2 Scala sources and 1 Java source to .../so22717836annotation/target/scala-2.10/classes...
[info] Running HelloWorld
Hello, world!
这里是代码:
$ for i in `echo build.sbt; find src/main -type f`; do \
echo '###' $i '###'; echo; perl -pe 's/^/ /' $i; echo; done
name := "so22717836"
version := "0.0.1"
scalaVersion := "2.10.4"
package test;
public @interface TestAnnotation {
String value();
}
import test._
@TestAnnotation(Constants.test + ", world!")
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
object Constants {
final val test = "Hello"
}