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

在Scala中不能失败的未来

发布时间:2020-12-16 18:39:42 所属栏目:安全 来源:网络整理
导读:在 Scala中是否存在一个不能失败的未来概念? 我正在改变未来[结果],这可能会失败 – 因此我处理失败和成功进入未来[Option [String]],携带从失败或成功状态派生的可选错误消息.到现在为止还挺好. 现在,我想正式(即,在类型系统的帮助下)记住,这个未来将永远
在 Scala中是否存在一个不能失败的未来概念?

我正在改变未来[结果],这可能会失败 – 因此我处理失败和成功进入未来[Option [String]],携带从失败或成功状态派生的可选错误消息.到现在为止还挺好.

现在,我想正式(即,在类型系统的帮助下)记住,这个未来将永远保持成功,并且我将来不需要处理失败案例.

有一种聪明的方法吗?

解决方法

这不是类型标记的用途吗?

scala> type Tagged[U] = { type Tag = U }
defined type alias Tagged

scala> type @@[T,U] = T with Tagged[U]
defined type alias $at$at

scala> trait OK ; trait Uncertain
defined trait OK
defined trait Uncertain

scala> type Sure[A] = Future[A] @@ OK
defined type alias Sure

scala> type Unsure[A] = Future[A] @@ Uncertain
defined type alias Unsure

scala> val f = Future.successful(42).asInstanceOf[Sure[Int]]
f: Sure[Int] = Future(Success(42))

然后

scala> object X { def p(f: Sure[_]) = "sure" ; def p(f: Unsure[_])(implicit d: DummyImplicit) = "unsure" }
defined object X

scala> X.p(f)
res1: String = sure

当然,它在地图下并不确定.

(编辑:李大同)

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

    推荐文章
      热点阅读