2014-12-31 42 views
3

我在閱讀關於隱式轉換的this tutorial忽略scala的高級功能警告的後果是什麼?

我進入了REPL此代碼-feature開關:

object Rational { 
    implicit def intToRational(x: Int): Rational = new Rational(x) 
} 

而且我得到了這樣的警告:

<console>:9: warning: implicit conversion method intToRational should be enabled 
by making the implicit value scala.language.implicitConversions visible. 
This can be achieved by adding the import clause 'import scala.language.implicitConversions' 
or by setting the compiler option -language:implicitConversions. 
See the Scala docs for value scala.language.implicitConversions for a discussion 
why the feature should be explicitly enabled. 
       implicit def intToRational(x: Int): Rational = new Rational(x) 

但是當我運行這段代碼的隱式轉換正常工作:

scala> 12 * new Rational(1, 3) 
res5: Rational = 4/1 

因此,如果我不遵循警告建議,那麼會有不好的後果嗎? (即添加import語句或設置編譯器選項)

+2

只有一個結果是編譯器輸出中的警告太多,這只是令人討厭的。 –

+0

導入功能並不會有任何警告,您只需要明確。 –

+2

許多不需要的小貓每年春天都出生。如果你可以等到那時候,也許看起來並不是那麼可怕,其中一個人每次發出警告都會被殺死。 –

回答

3
  1. 可能在將來的一些版本中,如果不添加import子句,代碼將不會編譯。或者如果你想使用-Xfatal-warnings

  2. 對於其他功能警告(尤其是反射呼叫),您可能實際上想要消除它們;這並不適用於這個特定的警告。如警告所示,閱讀文檔。