Groovy and the safe dereference operator

When going through view code generated by Grails I noticed heavy use of the question mark operator when referencing properties. After browsing around I discovered that it is called the 'safe dereference operator'

Sounds more complex then it is, it just makes sure that consequent methodcalls are only executed when the object on which it is called actually exists:

GROOVY:
  1. user?.email?.encodeAsHTML()

In the case above the 'email' property will only get read when the 'user' object actually exists and the 'encodeAsHTML()' will only get called when the 'email' property actually has a value. Cool!


0 Responses to “Groovy and the safe dereference operator”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


smashmouth Joshua Bloch at JavaOne2008 sea_lion Community One Keynote nearby hotel Rudie Hotel room javaone 2008 goodybag Golden Gate Tim Bray introducing the (J)Ruby panel pub Charles Nutter & Guillaume Laforge Java + You on a cab javaone2008 keynote Acme Anvile at CommunityOne Keynote Stage being build in the nearby park Moscone Center - JavaOne Cable Car line golden_gate_warning_sign Stretched Limo
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.