[ 
https://issues.apache.org/jira/browse/GROOVY-8705?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16551540#comment-16551540
 ] 

Paul King edited comment on GROOVY-8705 at 7/21/18 6:27 AM:
------------------------------------------------------------

Yes, what you are observing is per current design as Jochen indicates.

In terms of consistency, it is better to think that Groovy lets you drop the 
parenthesis on a method call when you wouldn't get an ambiguous result. This is 
a case where you can't avail yourself of the short-cut. list[index] or map[key] 
work with or without a space and take precedence in the grammar rules over a 
method call hence method([list_arg]) requires the parenthesis to remain.

I should add that the error message might also seem a little strange until you 
know of some of Groovy's GPath short cuts:
{code}
assert [0..5, 10..15, 20..<25].size() == 3
assert [0..5, 10..15, 20..<25]*.size() == [6, 6, 5]
assert [0..5, 10..15, 20..<25]*.getTo() == [5, 15, 24]
assert [0..5, 10..15, 20..<25]*.to == [5, 15, 24]
assert [0..5, 10..15, 20..<25].to == [5, 15, 24]
{code}
The first invokes the {{size}} method of the list (1). The spread-dot operator 
allows you to for instance use the {{size}} operator on all members of a list 
(2). You can also use the spread-dot operator with properties/property getters 
(3,4). For the special case of a property which doesn't exist on the list 
itself, it will automatically spread across the list items. Hence the error 
message referring to {{Integer}} rather than {{List}}.



was (Author: paulk):
Yes, what you are observing is per current design as Jochen indicates.

In terms of consistency, it is better to think that Groovy lets you drop the 
parenthesis on a method call when you wouldn't get an ambiguous result. This is 
a case where you can't avail yourself of the short-cut. list[index] or map[key] 
work with or without a space and take precedence in the grammar rules over a 
method call hence method([list_arg]) requires the parenthesis to remain.

> When add method of java.util.List is invoked with a list parameter with 
> paranthesis removed , the method fails.
> ---------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-8705
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8705
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Sarath Kumar
>            Priority: Minor
>
> Consider following Groovy snippet
> def myList = [100, 200, 300]
> myList.add [400]
> println myList
> The above snippet fails with following output : 
> Caught: groovy.lang.MissingPropertyException: Exception evaluating property 
> 'add' for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: 
> No such property: add for class: java.lang.Integer
> groovy.lang.MissingPropertyException: Exception evaluating property 'add' for 
> java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such 
> property: add for class: java.lang.Integer
> at groovyDrill1.run(groovyDrill1.groovy:2)
> If I put parenthesis around the list parameter as myList.add ([400]) , the 
> method works fine.I am new to Groovy and find the optional paranthesis 
> feature inconsistent.
> Any status updates would be greatly appreciated, thanks!
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to