Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way of treating Option[Boolean]

Tags:

scala

In my Scala program, I am receiving some JSON.

One of the fields is an optional Boolean. If the field is missing, or if its value is false, I would like to return None. If its value is true, I would like to return Some(true).

SInce this is equivalent to converting Some(false) into None, I have defined the following function:

def boolmap(ob: Option[Boolean]) = if(ob == Some(false)) None else ob

It works, but it doesn't seem to be very idiomatic. Is there anything more elegant?

like image 927
Eduardo Avatar asked Jun 18 '13 22:06

Eduardo


1 Answers

This is ob.filter(identity). I'm not sure whether that's clearer, but it's shorter.

like image 84
Rex Kerr Avatar answered Oct 02 '22 08:10

Rex Kerr