我在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
答案 0 :(得分:5)
这是因为案例类为您定义了==
方法,它比较了案例类中的每个字段。所以即使Step("a", println("1")) == Step("a", println("2"))
函数不相同,Unit
也是如此。
答案 1 :(得分:2)
你真的需要f: Unit
,这是非常不可能的。毕竟,Unit
只有一个值:()
。
我想你可能会考虑这样做:
Step("Debugging", println("here"))
确实尊重所有类型,但在调用run
或应用run
的返回值时 NOT 打印“here”。相反,它会在您初始化Step
时打印“此处”,然后将返回值[{1}}传递给()
。在您拨打f
时,它将无效。
也许你想要这个:
run
甚至:
case class Step(name: String, f: => Unit) {
def run = {() => f}
}