1

我在 Scala Play Framework 中有以下代码:

  case class Step(name: String, f: Unit) {
    def run = {() => f}
  }

编译器给了我一个奇怪的警告

comparing values of type Unit and Unit using '==' will always yield true
4

2 回答 2

5

这是因为案例类==为您定义了一个方法,该方法会比较案例类中的每个字段。Step("a", println("1")) == Step("a", println("2"))即使Unit功能不一样也是如此。

于 2012-08-23T21:32:32.447 回答
2

你真正需要的可能性很小f: Unit。毕竟,Unit只有一个值:().

我想你可能正在考虑这样做:

Step("Debugging", println("here"))

实际上,它尊重所有类型,但在调用或应用返回值时不会打印“here” 。相反,它会在您初始化 时打印“here” ,然后将返回值 , 传递给。在你打电话的时候,它什么也不做。runrunStep()frun

也许你想要这个:

case class Step(name: String, f: => Unit) {
  def run = {() => f}
}

甚至:

case class Step(name: String, f: => Unit) {
  def run = f
}
于 2012-08-23T22:48:30.513 回答