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

后端 未结 7 1886
旧时难觅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:17

    Similar to Scalaz, the Typelevel cats ecosystem has the mouse package with option:

    scala> true.option("Its true!")
    res0: Option[String] = Some(Its true!)
    

提交回复
热议问题