Hi,

I have a DSL where I want to pass named arguments like follows:

def foo(String a, String b, String c, Closure d) {
}

foo(a=“a”, c=“c”, b=“b”) {
}

I’m aware that named arguments are supported by receiving the arguments with a 
map:

def foo(Map<String, String> obj, Closure c) {
}

foo(a:"a", c:”c", b:”b") {
}

The problem with this approach is that for a user of this DSL, it is not 
obvious which parameters are expected and what their names are (beside 
documenting it, but who is going to read that ;-)). 

How can I achieve named function arguments with “real" syntax in Groovy? Is 
there an alternative to using maps for this?

By the way, I think it is kind of misleading for a beginner that the following 
compiles but does not pass the arguments in the intended order:

foo(a="a", c=“c", b=“b") {
  // a == a, b == c, c==b
}

Is this intended behaviour?

Thanks in advance,
Michael



Reply via email to