Scala返回选项值(Scala returning option value)

编程入门 行业动态 更新时间:2024-10-12 03:21:25
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.

更多推荐

本文发布于:2023-07-30 15:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338847.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   Scala   option   returning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!