ObservableValue.map special-cases the null value

Michael Strauß michaelstrau2 at gmail.com
Tue May 16 22:25:27 UTC 2023


I'm trying to map an ObservableValue such that `null` is mapped to
`false` and any other value is mapped to `true`. The following line of
code should do the trick:
ObservableValue<Boolean> mappedValue = nullableValue.map(x -> x != null);

It turns out that this doesn't work, since `map` doesn't pass `null`
to the mapping function. Instead of calling the mapping function, it
always returns `null`. This is not technically a bug, as `map` is
specified to have this behavior. It is a bit unfortunate, though;
`null` is a perfectly valid value in many cases and its special
treatment comes off as a bit of a surprise.

I'm wondering whether we need a `mapNullable` operator that does what
I intuitively expecting `map` to do, which is just unconditionally
passing the value to the mapping function.


More information about the openjfx-dev mailing list