Search code examples
groovylanguage-design

Why are parentheses optional in Groovy only if you don't need the return value?


For instance this:

groovy:000> Arrays.asList 1,2,3,4,5
===> [1, 2, 3, 4, 5]

works, because the value is not needed.

But when the return value is assigned to a variable:

groovy:000> a = Arrays.asList 1,2,3,4,5
ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, groovysh_parse: 1: unexpected token: 1 @ line 1, column 19.
   a = Arrays.asList 1,2,3,4,5
                     ^

1 error

        at java_lang_Runnable$run.call (Unknown Source)

Fails.

To make it run you need the parentheses.

groovy:000> a = Arrays.asList( 1,2,3,4,5)
===> [1, 2, 3, 4, 5]

Is there a design reason behind this? Or is it just the way it was implemented?


Solution

  • I don't know the answer historically, but note:

    Your example should work with Groovy 1.8 beta3+