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语句或设置编译器选项)
只有一个结果是编译器输出中的警告太多,这只是令人讨厌的。 –
导入功能并不会有任何警告,您只需要明确。 –
许多不需要的小猫每年春天都出生。如果你可以等到那时候,也许看起来并不是那么可怕,其中一个人每次发出警告都会被杀死。 –