Hi,
we currently have two ways to represent optional values in Scala: Option[A] and A | Null. I think it’s safe to say that the former is generally preferable: it can be nested and it has a useful API with methods like getOrElse, fold etc.. Therefore I’d like to generally reserve A | Null for interoperability (Java/JS/native) use cases.
However A | Null has an advantage: it’s more convenient for methods with optional parameters. def foo(x: Int | Null = null) can be called as foo(42), while def foo(x: Option[Int] = None) needs to be called as foo(Some(42)).
But we can easily get rid of the wrapping: add given[A] => Conversion[A, Option[A]] = Some(_) to the Option companion object. I used to think that having this conversion is bad because then you’d be able to call Option methods on all objects. But that’s not the case when you place the conversion in the companion object: it’ll only be applicable in places where an Option is expected anyway. And on top of that, implicit conversions will soon require into, making any unwanted conversions even less likely. Unintended use of this conversion seems extremely unlikely under these conditions.
We could also go one step further: add given[A] => Conversion[A | Null, Option[A]] = Option(_) to the Option companion object. This would make interop a little easier, allowing you to use nullable references from Java/JS/native in places where Option is expected.
What do you think?