groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <>
Subject Re: Implementing a DSL using groovy.util.DelegatingScript
Date Tue, 16 Aug 2016 09:46:52 GMT

On 16.08.2016 10:45, Gerrit Telkamp wrote:
> I'm using groovy.util.DelegatingScript to realize a simple DSL. It works
> quite well and the implementation was straight forward.
> Now I would like to extend my DSL by more words. Usually, this would be
> done by implementing each word as method in the delegate class.
> Instead I would like to split the code into several delegate classes.
> Having a look into the source, it seems to me that it would be easy to
> iterate a list of delegate classes and branching into a the first method
> that has been found first.
> What do the groovy developers say about this? Have I missed something,
> or is this something that is alrady planned? A future idea would be to
> re-use the DSL classes in an application-specific combination for other
> projects.

actually the class you delegate to can do this logic. Like:

class MyDelegate {
   def listOfDelegates = []

   def invokeMethod(String name, Object args) {
     listOfDelegates.each {
       try {
         return it."$name"(*args)
       } catch (MissingMethodException mme) {
         // swallow the exception here?
     throw new MissingMethodException(..)

and then
new DelegatingScript(delegate: new MyDelegate(list:[delegate1, 

You could also realize a chain of delegates:

class EndPoint {}
class BasicDSL extends DelegatingScript {...}
class ExtendedDSL extends DelegatingScript {...}

new DelegatingScript(delegate:new ExtendedDSL(delegate: new 
BasicDSL(delegate:new EndPoint())))

or combine the approaches... or even:

class MyDelegate extends DelegatingScript{
   MyDelegate(listOfDelegates = []) {
     delegate = listOfDelegates.inject(new EndPoint()) { prev, it -> 
it.newInstance(delegate: prev) }
new DelegatingScript(delegate:new MyDelegate(BasicDSL, ExtendedDSL))

bye Jochen

View raw message