gpt4 book ai didi

Scala 构造函数弃用

转载 作者:行者123 更新时间:2023-12-04 22:58:14 26 4
gpt4 key购买 nike

我在 Scala 中有一个类,目前以标准方式构建:

class Test( int : Int )
{
override def toString() = "Test: %d".format( int )
}

但是,我想通过伴随对象转到间接构造。由于我正在修改的库被其他人使用,我不想立即将构造函数设为私有(private)。相反,我想弃用它,然后在人们有机会改变他们的使用方式后返回并将其设为私有(private)。所以我像这样修改了我的代码:
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 )
}

但是,这会弃用整个类。
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

有谁知道 Scala 是否支持弃用构造函数,如果支持,它是如何完成的?

最佳答案

This thread似乎描述了解决方案:

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

不过现在还不能试。

关于Scala 构造函数弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com