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


golden_gate_warning_sign Hotel room Java + You on a cab Joshua Bloch at JavaOne2008 Charles Nutter & Guillaume Laforge Stretched Limo Scribbled Sun Logo smashmouth Community One Keynote Acme Anvile at CommunityOne Keynote javaone 2008 goodybag Greenland Rudie alcatraz sea_lion Tim Bray introducing the (J)Ruby panel nearby hotel Okke en Rudie Stage being build in the nearby park pub
View more photos >

Categories



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