Optional.orElse(null) ambiguity (original) (raw)

Joe Bowbeer joe.bowbeer at gmail.com
Mon Jan 14 10:50:09 PST 2013


I like:

orElseFrom(Supplier)

I don't like orElseGet(Supplier) because whenever I read orElse(other), I "translate" it as getOrElse(other).

On Mon, Jan 14, 2013 at 10:23 AM, Brian Goetz <brian.goetz at oracle.com>wrote:

Null is a valid value for orElse(T) but not for orElse(Supplier). So we care about this ambiguity (there are other similar ones we don't care about, when null isn't a valid value for either option.)

orElseNull is a possibility. Alternately, orElseGet(Supplier) ? orElseFrom(Supplier)? On Jan 13, 2013, at 10:00 PM, Joe Bowbeer wrote: > In b72, optional.orElse(null) is ambiguous because it matches both of the orElse forms: > > T orElse(T other) > T orElse(Supplier other) > > For example, this won't compile: > > return stream.max(comparator).orElse(null); > > > Optional needs a concise way to return null, so I suggest: > > T orElseNull() > > --Joe



More information about the lambda-libs-spec-observers mailing list