gpt4 book ai didi

scala - 忽略scala的高级特性警告会有什么后果?

转载 作者:行者123 更新时间:2023-12-01 23:48:45 25 4
gpt4 key购买 nike

我正在阅读 this tutorial关于隐式转换。

我使用 -feature 开关在 REPL 中输入了这段代码:

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

那么如果我不遵循警告建议会有不好的后果吗? (即添加导入子句或设置编译器选项)

最佳答案

  1. 可能在未来的某个版本中,如果不添加 import 子句,代码将无法编译。或者,如果您想使用 -Xfatal-warnings

  2. 对于其他功能警告(尤其是反射调用),您实际上可能想要消除它们;这并不真正适用于这个特定的警告。按照警告建议阅读文档。

关于scala - 忽略scala的高级特性警告会有什么后果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723976/

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