Groovy可选括号使用(Groovy optional parenthesis use)
我正在尝试编写一个允许语法的DSL:
foo ["a", "b"], bar: { true }
我认为应该像定义一个接受属性映射作为第一个参数的方法一样容易,例如:
def foo(Map attr, List blar) { ... }
但似乎这种语法会导致问题,想知道是否有人可以解释原因,以及是否有一个解决方案允许像顶部的行那样的无语言语法。
样本groovysh执行:
groovy:000> def foo(Map attr, List blar) { println attr; println blar; } ===> true groovy:000> foo ["a", "b"], bar: { true } ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: groovysh_parse: 1: expecting EOF, found ',' @ line 1, column 15. foo ["a", "b"], bar: { true } ^ 1 error at java_lang_Runnable$run.call (Unknown Source)
I'm trying to write a DSL that allows syntax like:
foo ["a", "b"], bar: { true }
Which I figured should be as easy as defining a method that accepts an attribute map as the first argument, e.g.:
def foo(Map attr, List blar) { ... }
But it seems like this syntax causes problems, wondering if anyone can explain why, and if there's a solution to allowing paren-free syntax like the line at the top.
Sample groovysh execution:
groovy:000> def foo(Map attr, List blar) { println attr; println blar; } ===> true groovy:000> foo ["a", "b"], bar: { true } ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: groovysh_parse: 1: expecting EOF, found ',' @ line 1, column 15. foo ["a", "b"], bar: { true } ^ 1 error at java_lang_Runnable$run.call (Unknown Source)
更新时间:2022-09-11 17:09