You can do
def foo(s: Option[String]) = s.forall(_.isEmpty)
or
def foo(s: Option[String]) = s.fold(true)(_.isEmpty)
foldhas 2 parameters in different lists. The first is for the case None, and the other is evaluated, if any String.
Personally, I would prefer the first solution, but in the second one it is very clear what you want to return truein the case None.
Some examples:
foo(Some("")) //true
foo(Some("aa")) //false
foo(None) //true
source
share