So there’s been quite a few comments along the lines of “this only works for Option
s”, “this privileges Option
”, and “for
-comprehensions are more general, this is only a special case”.
Option
s are special, they should be special, and they are a case worth specializing for:
- They are how we deal with
null
. -
0..1
is different in kind not degree from0..N
, sofor
comprehensions are not more general, they are different.
Option
s are pervasive in Scala, yet their usage is still cumbersome.
Adding if
-comprehensions and def foo(s: String?)
callable with foo(myString)
and foo(myStringOpt: _?)
as I suggested years ago (please ignore the elvis operator second part of that suggestion) would go a long way towards making Option
s in Scala more ergonomic.
And that is a goal worth pursuing.