Golo supports null-safe methods invocations using the "Elvis" symbol: ?:.
Suppose that we invoke the method bar() on some reference foo: foo: bar(). If foo is null,
then invoking bar() throws a java.lang.NullPointerException, just like you would expect in Java.
By contrast:
foo?: bar() simply returns null, and
null?: anything() returns null, too.
This is quite useful when querying data models where null values could be returned. This can be
elegantly combined with the orIfNull operator to return a default value, as illustrated by the
following example:
let person = dao: findByName("Mr Bean")
let city = person?: address()?: city() orIfNull "n/a"This is more elegant than, say:
let person = dao: findByName("Mr Bean")
var city = "n/a"
if person isnt null {
let address = person: address()
if address isnt null {
city = address: city() ofIfNull "n/a"
}
}The runtime implementation of null-safe method invocations is optimistic as it behaves
like a try block catching a NullPointerException. Performance is good unless most invocations
happen to be on null, in which case using ?: is probably not a great idea.