Scala:无法将 class 方法参数标记为 var/val
Scala: unable to mark class method arguments as var/val
class Time(var h: Int, val m: Int) {
def before(val other: Time) = { //compile error due to keyword val
(this.h < other.h) || (this.m < other.m)
}
}
如何将方法 before 中的参数 other 标记为 var/val?如果我在 other 之前删除 val,它会成功编译。
您不能修改对 other
的引用,因为它是函数的参数。
def before(val other: Time) = ...
将等同于(如果合法)
def before(other: Time) = ...
如果您想要 var
,只需在函数内创建它:
def before(other: Time) = {
var otherVar = other
...
}
class Time(var h: Int, val m: Int) {
def before(val other: Time) = { //compile error due to keyword val
(this.h < other.h) || (this.m < other.m)
}
}
如何将方法 before 中的参数 other 标记为 var/val?如果我在 other 之前删除 val,它会成功编译。
您不能修改对 other
的引用,因为它是函数的参数。
def before(val other: Time) = ...
将等同于(如果合法)
def before(other: Time) = ...
如果您想要 var
,只需在函数内创建它:
def before(other: Time) = {
var otherVar = other
...
}