我正在使用 Groovy 类别在一些 DSL 下工作,我想找到一种方法来使用我的 DSL 和 groovy shell,而无需use(MyCategory){ myObject.doSomething() }
为每个命令显式编写。
例如,假设我有以下玩具类别:
class MyCategory {
static Integer plus(Integer integer, String string){
return integer + Integer.valueOf(string)
}
}
groovysh
然后,我可以通过以下方式使用此类别:
groovy> use(MyCategory){ 2 + '3' } //gives 5
那么,有没有办法MyCategory
为所有命令全局设置groovysh
,所以每次都不需要将我的命令包装在use(MyCategory) { ... }
? 例如:
groovy> useGlobally(MyCategory); //call something like this only once
groovy> 2 + '3' //automatically uses MyCategory and gives 5