为什么只有当您不需要返回值时,Groovy 中的括号才是可选的?

Osc*_*Ryz 2 groovy language-design

例如这个:

groovy:000> Arrays.asList 1,2,3,4,5
===> [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

有效,因为不需要该值。

但是当返回值赋给变量时:

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)
Run Code Online (Sandbox Code Playgroud)

失败了。

要使其运行,您需要括号。

groovy:000> a = Arrays.asList( 1,2,3,4,5)
===> [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

这背后有设计原因吗?或者这只是它的实施方式?

Mic*_*ter 5

我不知道历史上的答案,但请注意:

您的示例应该适用于 Groovy 1.8 beta3+