Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] Safe dereference for nested maps

pexatus

2008-08-24

Replies: Find Java Web Hosting

Author LoginPost Reply

I have a map of maps, so that it can be used like a generalization of a 2D
array; i.e.

def map = ['a':['b':'c'], 'd':['e':'f']]

I want to use the safe dereference operator, but it does not work with the
subscript operator. In other words, I want to do this;

if (map['a']?['b'] != null) {
// process map['a']['b']
}

But this does not work. Instead, I do this:

if (map.getAt('a')?.getAt('b') != null) {
// process map['a']['b']
}

This works, but is a little wordy, and it seems like since getAt is what the
subscript operator is translated to, the top line should work. Is there a
shorter syntax for what I want to do?

Thanks,
Dave
--
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.