Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy function call omiting the parentheses

Tags:

gradle

groovy

According to the gradle documentation/section 13.5.2 we can omit parentheses in a method call:

Parentheses are optional for method calls.

But it seems it doesn't work when we try to apply the java plugin. If a script contains the following line:

apply [plugin: 'java']

We'll get the error:

 Maybe something should be set in parentheses or a comma is missing?
 @ line 1, column 8.
     apply [plugin: 'java']
            ^

But if we put this Map-literal into a parentheses it'll work fine.

apply([plugin: 'java'])

So we can't omit the parentheses when the argument is a Map, can we?

like image 969
St.Antario Avatar asked Dec 15 '22 17:12

St.Antario


1 Answers

As the specification says, parentheses can be omitted when there is no ambiguity. I suspect the ambiguity in this case arises because the statement without parentheses looks a lot like array index syntax and the parser has trouble working out whether you are calling a method named 'apply' or trying to do something with an array named 'apply'.

Personally, this is why I tend to always use parentheses - if the parser can't work it out I'm sure another programmer reading the code won't either.

like image 70
BarrySW19 Avatar answered Dec 24 '22 01:12

BarrySW19