Reputation: 3828
Now I create Option
using if
condition:
if (lastTime > 0)
Some(lastTime)
else
None
is it possible to simplify such expression?
Upvotes: 1
Views: 51
Reputation: 14224
Also, you can add an extension method to Boolean
s:
implicit class BooleanExtension(val bool: Boolean) extends AnyVal {
def option[T](value: T): Option[T] =
if (bool) Some(value) else None
}
Usage: (lastTime > 0).option(lastTime)
And in case you are using the Scalaz library, it already has this extension method (option
) defined.
Upvotes: 1