加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 将未来[单位]分配给单位

发布时间:2020-12-16 19:11:31 所属栏目:安全 来源:网络整理
导读:为什么这会编译 scala import scala.concurrent.Futureimport scala.concurrent.Futurescala val f: Unit = Future.successful(())f: Unit = () 我希望编译器抱怨这个任务. 解决方法 这称为“价值丢弃”.引用 scala specification (6.26.1): Value Discardi
为什么这会编译

scala> import scala.concurrent.Future
import scala.concurrent.Future

scala> val f: Unit = Future.successful(())
f: Unit = ()

我希望编译器抱怨这个任务.

解决方法

这称为“价值丢弃”.引用 scala specification (6.26.1):

Value Discarding

If e has some value type and the expected type is Unit,e is converted to the expected type by embedding it in the term { e; () }.

换句话说,任何值,无论其类型如何,都会隐式转换为Unit,从而有效地丢弃它.

您希望收到有关此类丢弃的警告(在某些情况下可能会隐藏错误),您可以将-Ywarn-value-discard选项传递给编译器.然后,每次为方法调用方法时都必须显式返回(),但该方法确实返回非Unit值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读