Groovy DSL 使用括号?
我有一个像这样的 DSL 脚本:
entity(attribute1:"one", attribute2:"two")
到目前为止一切都很好。我运行脚本并将脚本的委托设置为定义实体的类,并且该类处理所有内容。
现在我想这样做:
entity(attibute1:(subattribute1:"one", subattribute2:"two"))
这在语法上是否可能?由于 (subattribute1:"one", subattribute2:"two") 本身没有任何意义,我假设没有,尽管我想知道是否有一些我不知道的 Groovy 魔法允许这样做。
我也不想这样做
entity(attibute1:[subattribute1:"one", subattribute2:"two"])
即使我知道这有效, 。只是语法偏好。
I have a groovy DSL script like this:
entity(attribute1:"one", attribute2:"two")
so far so good. I run the script and set the script's delegate to a class where entity's defined, and the class handles everything.
Now I want to do this:
entity(attibute1:(subattribute1:"one", subattribute2:"two"))
Is this somehow syntactically possible? Since (subattribute1:"one", subattribute2:"two") itself doesn't mean anything, I'm assuming not, though I'm wondering if there are some Groovy magic that I'm not aware of that allows this.
And I don't want to do
entity(attibute1:[subattribute1:"one", subattribute2:"two"])
even though I know that works. Just a syntax preference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你必须使用方括号(正如你所说你不想要的)。
第一个示例:
是实际调用的快捷方式:
因此,您要么需要方括号,(表示
attribute1
键包含映射,或者您需要在大括号前添加另一个方法名称例如:No, you have to use the square brace (as you have said you don't want).
The first example:
is a shortcut for actually calling:
So, you would either need the square braces, (to signify the
attribute1
key contains a map, or you would need to prefix the brace with another method name such as: