Optional.orElse(null) ambiguity

Brian Goetz brian.goetz at oracle.com
Mon Jan 14 10:23:56 PST 2013


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<T> 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-experts mailing list