nitlang / nit

Nit language
http://nitlanguage.org
Apache License 2.0
239 stars 65 forks source link

Safe call operator #2761

Closed privat closed 5 years ago

privat commented 5 years ago

A long time ago, there was a proposal to have a safe call operator in Nit #1274 x?.foo that executes the call if x is not null and returns null otherwise (instead of aborting).

This was refused because at the time, the syntax x?.foo was considered weird and not POLA. Moreover, what was proposed was a more general version of the concept that could be used everywhere, not only as a receiver that made the semantic quite complex and even less POLA.

Nowadays, most languages have adopted it https://en.wikipedia.org/wiki/Safe_navigation_operator so the syntax and behavior is not as weird as before.

This operator is nice in the context of Nit with nullable types since if avoids to use chained if x != null then x.foo and plays nicely with the type system.

Currently, only explicit dotted calls are handled; e.g. x?.foo, I'm not sure if something should be done with other call constructions like x + y or x[y].

privat commented 5 years ago

reroll: applied suggestions and added comments