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語句或設置編譯器選項)
只有一個結果是編譯器輸出中的警告太多,這只是令人討厭的。 –
導入功能並不會有任何警告,您只需要明確。 –
許多不需要的小貓每年春天都出生。如果你可以等到那時候,也許看起來並不是那麼可怕,其中一個人每次發出警告都會被殺死。 –