groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gerrit Telkamp <g.telk...@domologic.de>
Subject Re: Implementing a DSL using groovy.util.DelegatingScript
Date Tue, 16 Aug 2016 14:11:25 GMT
Jochen, thank you for your approach!
It seems that question was a bit incomplete.
I should have asked: Is it planned to include a list of Delegate Classes 
by groovy itself, or is the preferred way to "roll your own" on 
application level by the existing groovy.util.DelegatingScript?
BTW, the "MissingMethodException" has been used by other DSLs like Spock 
etc., but in my opinion the newer DelegateScript class is a much more 
elegant way...
Thank you,
Gerrit.

Am 16.08.2016 um 11:46 schrieb Jochen Theodorou:
>
>
> 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, 
> delegate2,...]))
>
> 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
>
>
>


Mime
View raw message