pau*_*ies 13 constructor annotations scala deprecated
我在Scala中有一个类,它目前以标准方式构建:
class Test( int : Int )
{
override def toString() = "Test: %d".format( int )
}
Run Code Online (Sandbox Code Playgroud)
但是,我想通过伴侣对象转移到间接构建.由于我正在修改的库被其他人使用,我不想直接将构造函数设为私有.相反,我想弃用它,然后在人们有机会改变其使用情况后回来并将其设为私有.所以我修改了我的代码:
object Test
{
def apply( int : Int ) = new Test( int )
}
@deprecated( "Don't construct directly - use companion constructor", "09/04/13" )
class Test( int : Int )
{
override def toString() = "Test: %d".format( int )
}
Run Code Online (Sandbox Code Playgroud)
但是,这会弃用整个班级.
scala> Test( 4 )
<console>:10: warning: class Test in package foo is deprecated: Don't construct directly - use companion constructor
val res0 =
^
res0: com.foo.Test = Test: 4
Run Code Online (Sandbox Code Playgroud)
有没有人知道Scala是否支持弃用构造函数,如果支持,它是如何完成的?
Jen*_*der 10
这个线程似乎描述了解决方案:
object Test
{
def apply( int : Int ) = new Test( int )
}
class Test @deprecated( "Don't construct directly - use companion constructor", "09/04/13" ) ( int : Int )
{
override def toString() = "Test: %d".format( int )
}
Run Code Online (Sandbox Code Playgroud)
但是现在不能尝试.