我有Scala中的一個類,這是目前在標準方式構造:Scala的構造棄用
class Test(int : Int)
{
override def toString() = "Test: %d".format(int)
}
然而,我想通過一個同伴對象移動到間接施工。當我正在修改的庫被其他人使用時,我不想立即將構造函數私有化。相反,我想棄用它,然後在人們有機會更改其使用情況後再回來並將其設爲私有。所以我修改我的代碼是這樣的:
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是否支持對構造函數的棄用,如果有的話,它是如何實現的?
(編輯)這確實有效,但它不是絕對理想的,因爲在導入此類時您會收到棄用警告,因爲伴隨對象會調用已棄用的構造函數 - 任何想法都會繞過該構造函數? – paulmdavies 2013-04-09 08:42:17
你確定嗎?編譯'Test'時應該得到一個警告,但是在導入它時不會。 – 2013-04-09 09:19:36
你是對的 - 只有在編譯。 – paulmdavies 2013-04-09 09:39:52