【发布时间】:2015-06-07 04:31:41
【问题描述】:
我有一个复杂的 Groovy 类别,它定义了我的 DSL 语言。例如,它包含以下内容:
class MyDSL {
static Expr getParse(String expr) {
Parser.parse(expr)
}
static Expr plus(Expr a, Expr b){
a.add(b)
}
...
}
MyDSL 类别包含许多类型对象的 DSL 元素(不仅是 String 和 Expr)。我可以使用 use 关键字在另一个类中使用我的 DSL:
class MyAlgorithm {
Expr method1(Expr expr) {
use(MyDSL) {
def sum = 'x + y'.parse
return sum + expr
}
Expr method2(String t) {
use(MyDSL) {
def x = t.parse
return method1(x) + x
}
...
}
我可以做些什么来避免在每种方法中每次都写use关键字吗?我还想在我的 IDE 中进行代码完成和语法高亮(IntelliJ 可以完美识别 use 中的 DSL)。
在单元测试中有类似的问题Use Groovy Category implicitly in all instance methods of class 关于避免use,但我需要在主类中做同样的事情。
【问题讨论】:
标签: groovy metaprogramming dsl