为什么不映射到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)))更多推荐
发布评论