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

scala – 有没有办法在理解中声明一个隐含的val?

发布时间:2020-12-16 09:32:04 所属栏目:安全 来源:网络整理
导读:我有一些代码嵌套调用flatMap像这样: foo.flatMap(implicit f = bar(123).flatMap(b = /* and so on... implicit f is still in scope here.*/)) 通常,人们会把它写成一个理解,这使得代码更加可读: for { f - foo b - bar(123) /* yet more method calls
我有一些代码嵌套调用flatMap像这样:

foo.flatMap(implicit f => bar(123).flatMap(b =>
  /* and so on... implicit f is still in scope here.*/
))

通常,人们会把它写成一个理解,这使得代码更加可读:

for {
  f <- foo
  b <- bar(123)
  /* yet more method calls that need f as an implicit parameter*/
}

但我需要f是隐含的,我没有看到一个方法来做这个理解。在那儿?当然我可以明确地传递f,但这将意味着再见漂亮的DSL。我会对Scala 2.9和2.10的答案感兴趣。

要清楚,我想做这样的事情,但不会编译:

for {
  implicit f <- foo
  b <- bar(123) //bar takes implicit argument
  /* yet more method calls that need f as an implicit parameter*/
}

编辑:也许一个功能要求是一个好主意?

EDIT2:这应该适用于所有可以用于理解的类型,所以不仅仅是通常的集合类型,如List或Seq,而且与Future。

解决方法

不,没有。有一张票: https://issues.scala-lang.org/browse/SI-2823

(编辑:李大同)

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

    推荐文章
      热点阅读