Simplify if (x) Some(y) else None?

后端 未结 7 1999
旧时难觅i
旧时难觅i 2021-02-05 02:46

This common pattern feels a bit verbose:

if (condition) 
  Some(result)
else None

I was thinking of using a function to simplify:



        
7条回答
  •  轮回少年
    2021-02-05 03:05

    Scalaz includes the option function:

    import scalaz.syntax.std.boolean._
    
    true.option("foo") // Some("foo")
    false.option("bar") // None
    

提交回复
热议问题