作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Groovy 开发 DSL,但遇到了以下问题。我有一个方法可以对具有给定参数的对象执行一些操作。
def run(x) {
[with:{ y -> foo(x,y) }]
}
run "thing" with "param" // evaluates to foo("thing","param")
现在,假设我想向我的 DSL 添加一个默认功能:
def runDefault(x) {
foo(x)
}
runDefault "thing" // evaluates to foo("thing")
有没有办法将两者组合成一个函数,使 with "param"
部分成为可选子句?我希望能够使用如下所示的 DSL:
run "thing" with "param" // should do foo("thing","param")
run "thing" // should do foo("thing")
最佳答案
如果您能够区分运行方法中的两个调用,您可以这样做:
def run(x) {
switch (x) {
case 'foo':
println "foo($x)"; break;
case 'bar':
[with:{ y -> println "bar($x,$y)" }]; break;
}
}
run "bar" with "param"
run "foo"
关于groovy - 向 Groovy DSL 添加可选子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660737/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!