[ 
https://issues.apache.org/jira/browse/GROOVY-3525?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-3525:
------------------------------
    Fix Version/s:     (was: 3.0.0)
                   3.0.0-alpha-1

> Null-safe dereference operator not available for subscript operator access to 
> properties or array/list elements
> ---------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-3525
>                 URL: https://issues.apache.org/jira/browse/GROOVY-3525
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 1.6, 1.6.1, 1.6.2, 1.6.3, 2.4.0-beta-4
>            Reporter: Marc Palmer
>            Assignee: Daniel Sun
>            Priority: Major
>             Fix For: 3.0.0-alpha-1
>
>
> The null-safe deref operator causes compiler errors if it is used with the 
> subscript operator.
> Take the following example:
> {code}
> def obj = [name:'Marc Palmer', occupation:'Whining user', ]
> println obj?.name
> {code}
> This is fine. However if you want to use the subscript operator for property 
> access:
> {code}
> def obj = [name:'Marc Palmer', occupation:'Whining user', ]
> def propName = 'name'
> println obj?[propName]
> {code}
> You get errors. Of course you can workaround this with:
> {code}
> def obj = [name:'Marc Palmer', occupation:'Whining user', ]
> def propName = 'name'
> println obj?.getAt[propName]
> println obj?."$propName"
> {code}
> But this is quite ugly if you are in a situation where you need/want to use 
> subscript, and also if this is done properly then we also get nullsafe deref 
> of arrays, lists and sets, especially if they are nested:
> {code}
> def myList = null
> println myList?[0]?['name']
> {code}
> Without this you have to write the equivalent of:
> {code}
> def myList = null
> println myList?.getAt(0)?.getAt('name')
> {code}
> Can't Groovy do this for us?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to