- 在某些情况下,不会报告任意值丢弃

时间:2017-11-20 10:40:11

标签: scala

在scalac帮助中,据说-Ywarn-value-discard应该在表达式返回非单位时发出警告,但结果未使用:

$ scalac -Y
-Ywarn-value-discard          Warn when non-Unit expression results are unused.

但是当我编译以下文件时:

// compile with scalac -Ywarn-value-discard Main.scala
object Main {
  def main(args: Array[String]) {
    hasReturnValue()
    println("hello") // this line silences the warning
  }
  def hasReturnValue() = {
    System.currentTimeMillis()
  }
}

我没有得到任何警告,但在我的理解中hasReturnValue()是一个表达式,它有非单位结果,并且结果肯定是未使用的。

看看Scala语言规范,我发现规范略有不同:

Value Discarding
If e has some value type and the expected type is Unit, e is converted to the expected type by embedding it in the term { e; () }.

似乎-Ywarn-value-discard的帮助文字中的措辞有点不正确。

我同意按照规范,我的示例不包含丢弃值,但是是否有警告会捕获所有未使用的返回值?

0 个答案:

没有答案