为什么不映射到Try在这种情况下删除Try(Why doesn't mapping over a Try remove the Try in this case)

编程入门 行业动态 更新时间:2024-10-24 22:22:48
为什么不映射到Try在这种情况下删除Try(Why doesn't mapping over a Try remove the Try in this case) // redis.get(key) returns a Option[A] redis.get(key).map(x => Try(x.toInt)).map(counter => doSomething(counter))

我收到一个错误,因为doSomething想要一个Int,但我传递了一个Try [Int]。

我认为Try上的映射会删除它,但它似乎没有工作。

我在这里想念的是什么?

// redis.get(key) returns a Option[A] redis.get(key).map(x => Try(x.toInt)).map(counter => doSomething(counter))

I am getting an error because doSomething wants an Int but I am passing a Try[Int].

I thought mapping on the Try would remove it, but it doesn't seem to be working.

What am I missing here?

最满意答案

Parens在错误的地方。 正如Michael Zajac所说,你正在映射Option ,而不是Try 。

固定:

redis.get(key).map(x => Try(x.toInt).map(counter => doSomething(counter)))

Parens in the wrong place. As Michael Zajac said, you're mapping the Option, not the Try.

Fixed:

redis.get(key).map(x => Try(x.toInt).map(counter => doSomething(counter)))

更多推荐

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

发布评论

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

>www.elefans.com

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