A static null-coalescing method

Emre Kaplan emr3kp at gmail.com
Tue Oct 29 10:40:39 UTC 2024


Hello,

I think that's covered by the instance method `Optional.orElse(def)`
>

Yes, but we should create an Optional object for calling orElse because it
is not static. For just null-checking, java.util.Objects seems better too.

And I checked discussions and saw this message:
>
> I took Remi's point about  the canonical coalescing operator not always returning non-null
>
> but the push seems to be in the direction of making sure the result is always non-null.
>
> Unless a coalescing operator is added, only requireNonNull methods are
mostly wanted then.

Roger Riggs <roger.riggs at oracle.com>, 28 Eki 2024 Pzt, 23:35 tarihinde şunu
yazdı:

> Hi,
>
> I guess I'd stop slacking off and figure out where that method goes.
> It's not clear it belongs on Optional, since it doesn't produce or consume
> an Optional.
> java.util.Objects is seems better as long as it can be named to avoid any
> confusion with other methods.
>
> :) Roger
>
> On 10/28/24 4:20 PM, Roger Riggs wrote:
>
> Hi,
>
> I think that's covered by the instance method `Optional.orElse(def)`
>
> Regards, Roger
>
> [1]
> https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/util/Optional.html#orElse(T)
>
>
>
> On 10/28/24 3:40 PM, Emre Kaplan wrote:
>
> Hello,
>
> I think java.util.Optional class should have a static method named like
> getOrDefault(Object o, Object default)
>
> In short, if an object is null, it should return the default object,
> otherwise the same object. And inside, default null check should be done.
> Same result can be achieved by method chaining of ofNullable and orElse,
> but then an object is created. I think it will be memory efficient because
> only one static method needs to be called. I don't know if it has been
> discussed before.
>
> Also there is Objects.requireNonNullElse(Object o1, Object o2)
> but the second parameter should be non-null.
>
> Thanks in advance..
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20241029/0ef595f1/attachment.htm>


More information about the jdk-dev mailing list