Safe navigation operator
Cay Horstmann
cay.horstmann at sjsu.edu
Wed Jan 16 20:04:16 UTC 2019
Sure, there are lots of legacy libraries where null indicates "empty".
But the language and API should nudge programmers towards a fruitful
direction--to use Optional.empty() and not null.
If your legacy library keeps dishing out null, talk to the authors and
let them know about your pain.
Cheers,
Cay
Le 16/01/2019 à 20:29, Jeyvison Nascimento a écrit :
> Hi Remi,
>
> it's true that we shouldn't return null in our methods but, in practical, I
> don't see it happening a lot. We can handle it and make our code behave
> nicely when we are developing something but we can't do the same when we
> are working with legacy/third-party code. When working with this kind of
> code, we still have to do all the null checking process (what can be a lot
> of if's) or use Optional.<Anything>.
>
> "null" can also be something meaningful to the app creator and, although it
> might not be best practice, we would be enforcing the perspective that
> "null" shouldn't be propagated.
>
>
> Shouldn't it be something that we should consider again?
>
> Em qua, 16 de jan de 2019 às 14:02, Remi Forax <forax at univ-mlv.fr> escreveu:
>
>> Hi Jeyvison,
>> The nullsafe navigation has been proposed as part of the project Coin (for
>> Java 7).
>>
>> As far as i remember, it has been rejected because it allows to seamlessly
>> propagate null in the code which is not the good solution, methods should
>> you never return null, so you should not have to propagate it.
>> Since then, Java 8 has introduced Optional which force the user of a
>> method that return Optional to take care of the fact that the method can
>> return no result.
>> The method Optional.map() (or flatMap()) is the equivalent of the nullsafe
>> navigation syntax.
>>
>> regards,
>> Rémi
>>
>> ----- Mail original -----
>>> De: "Jeyvison Nascimento" <jeynoronha at gmail.com>
>>> À: "amber-dev" <amber-dev at openjdk.java.net>
>>> Envoyé: Mercredi 16 Janvier 2019 16:13:02
>>> Objet: Safe navigation operator
>>
>>> Hi Folks :)
>>>
>>> Not sure if this is the best mail list to start it but I'd like to start
>> a
>>> discussion about safe navigation operator (i.e "?." in Kotlin) in java.
>>>
>>> Not sure if this was already talked about but if it wasn't I think it's
>>> something worth to investigate and explore.
>>>
>>> What do you people think about it?
>>>
>>> --
>>> Jeyvison Nascimento
>>
>
>
--
Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com
More information about the amber-dev
mailing list