I have a complicated Groovy category which defines my DSL language. For example it contains something like:
class MyDSL {
static Expr getParse(String expr) {
Parser.parse(expr)
}
static Expr plus(Expr a, Expr b){
a.add(b)
}
...
}
The MyDSL
category contains DSL elements for many types of objects (not only String
and Expr
). I can use my DSL in another class by using the use
keyword:
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
}
...
}
Can I do something to avoid writing of use
keyword each time in each method? I also would like still to have code completion and syntax highlighting in my IDE (IntelliJ perfectly recognises DSL inside use
).
There is similar question Use Groovy Category implicitly in all instance methods of class about avoiding use
in unit tests, but I need to do the same in main classes.