Summing up two options

前端 未结 4 1071
误落风尘
误落风尘 2021-02-08 22:10

Let\'s say I have two optional Ints (both can be Some or None):

val one : Option[Int] = Some(1)
val two : Option[Int] = Some(2)

My question is

4条回答
  •  耶瑟儿~
    2021-02-08 22:39

    You could try this:

    for( x <- one.orElse(Some(0)); y <- two.orElse(Some(0))) yield x+y
    

提交回复
热议问题