Groovy and the safe dereference operator
Published by peter August 17th, 2007 in grails, groovy.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:
-
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”
Please Wait
Leave a Reply