Scala返回选项值(Scala returning option value)
我希望这个函数能够返回选项值(以Option [None]的形式)或Option [something],但它只返回一个Unit。 我错过了什么?
def intersection(another: Interval){ var test: Option[Interval] = None if (this.isLaterThan(another) || another.isLaterThan(this)){ test } else { val a = this.start.later(another.start) val b = this.end.earlier(another.end) test=Some(new Interval(a, b)) test }The prolbem is that i'd like this function to return option value (in form of Option[None]) or Option[something], but it only returns an Unit. What am i missing?
def intersection(another: Interval){ var test: Option[Interval] = None if (this.isLaterThan(another) || another.isLaterThan(this)){ test } else { val a = this.start.later(another.start) val b = this.end.earlier(another.end) test=Some(new Interval(a, b)) test }最满意答案
你缺少=在第一个{之前。 没有=方法总是返回Unit 。 请注意,此语法实际上已被弃用,因为这是一个常见错误: https : //issues.scala-lang.org/browse/SI-7605 。
You are missing = before the first {. Methods defined without = always return Unit. Note that this syntax is actually deprecated precisely because this is a common error: https://issues.scala-lang.org/browse/SI-7605.
更多推荐
发布评论