我有一个这样的课程:
class Foo(x: Int, y: Int) {}
我想弃用x
。我试过这个:
class Foo(@deprecated("msg", "0.1") x: Int, y: Int)
在Scala 2.11中,我收到以下警告:
[warn] /tmp/zzz/src/main/scala/Foo.scala:1:12: no valid targets for annotation on value x - it is discarded unused. You may specify targets with meta-annotations, e.g. @(deprecated("msg", "0.1") @param)
[warn] class Foo(@deprecated("msg", "0.1") x: Int, y: Int) {
[warn] ^
[warn] one warning found
(我尝试了建议的语法,但这会导致编译错误)。我试过的语法适用于Scala 2.12。有没有办法以适用于2.11和2.12的方式弃用构造函数参数?
答案 0 :(得分:1)
通过使x
或var
符合val
来编译2.11:
class Foo(@deprecated("msg", "0.1") var x: Int, y: Int)
class Foo(@deprecated("msg", "0.1") val x: Int, y: Int)
然而,正如Andrey在评论中指出的那样,它不会弃用构造函数参数,而只会弃用相应的自动生成的成员变量。要弃用构造函数参数,请尝试将x
移动到辅助的已弃用构造函数:
class Foo(y: Int) {
@deprecated("msg", "0.1")
def this(x: Int, y: Int) = this(y)
}