Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B8A28200B67 for ; Tue, 16 Aug 2016 16:11:36 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B72D6160AA8; Tue, 16 Aug 2016 14:11:36 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 08A90160A74 for ; Tue, 16 Aug 2016 16:11:35 +0200 (CEST) Received: (qmail 84138 invoked by uid 500); 16 Aug 2016 14:11:35 -0000 Mailing-List: contact dev-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list dev@groovy.apache.org Received: (qmail 84119 invoked by uid 99); 16 Aug 2016 14:11:34 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Aug 2016 14:11:34 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id E6A5FC0C5A for ; Tue, 16 Aug 2016 14:11:33 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.999 X-Spam-Level: X-Spam-Status: No, score=0.999 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id FcFm7XohGKhA for ; Tue, 16 Aug 2016 14:11:31 +0000 (UTC) Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.17.10]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with ESMTPS id EF9A35F2F0 for ; Tue, 16 Aug 2016 14:11:30 +0000 (UTC) Received: from [10.0.254.124] ([93.223.245.182]) by mrelayeu.kundenserver.de (mreue102) with ESMTPSA (Nemesis) id 0LbIy2-1atTad1zNi-00kzQR for ; Tue, 16 Aug 2016 16:11:29 +0200 Subject: Re: Implementing a DSL using groovy.util.DelegatingScript References: <57B2D2B2.302@domologic.de> <57B2E10C.3050203@gmx.org> To: dev@groovy.apache.org From: Gerrit Telkamp Message-ID: <57B31F0D.5000300@domologic.de> Date: Tue, 16 Aug 2016 16:11:25 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: <57B2E10C.3050203@gmx.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:Fg8UUKwQSBOZz/bPMB9olGIOkLMOYhE+UjL4P5zaTC2wnz8/R4R 3ZvLc2lmOg70nQy0a+1AcOphi30AMt2qbMPKPg+ieWeEs7z7rb5eXKIxRuYxFeNtgxCddGd lpt6zonnIMCG9x/T0huiILh/YTLhabUGNKIQ0nUxoflEUsydl7rD9ZaISZz0ieGooAq649E p0ej5KuOuowJhVxCLauRA== X-UI-Out-Filterresults: notjunk:1;V01:K0:y0pOMKsw7v8=:cpBmc+x7ba0R+//LBEpRZJ jAj9knxmEzN8Yn73Js4fuhItxLUDVO5LpaWPl6VdXWHW9etwIcqfqtSJAJCdGqKNw8ho6Saco Ld0pwvfMSzqgPwEQxh1bwYZU0A7x4BS1GNon4/Zz5NZQeC4v9t8baGtc+WRyOT6MRFI947g2E vFa8LszE2Bxk+HrUSRDh0qBodnpgHcgIel5IU6ApBZk4lKzCELYuP09NEZFnQmfBcS5/t3zde UVnIIVIZqArphPUcM2c45yxoTLGguNiKEl1a3ACNvQz3pfqFFMKNlAzHRge46Z2SbBzwcmKx5 R5b6xmMgCruab5X6KOk81uHY+z0yE690TPw4CAyCY6hoiNTYV4uT0ebNwgpi3goB0+omsKrDX P6TQR04+gM99581SM4SeX6nGyowfjjzVnbPmavv53n0EQ1RdJllAxrikt4NQImMdGHsUvNuvj D+c2LIw/0ACk4ILkR8laZYEAdxRlrO1Bdlbp45x3pTUye3hDRyGmma+9csGIWPEbeGI/g7TpR k7NsHQ27a17H2CA/QcrVpl8CgujVuBnCG3HqW5LZjmgoQ4xVxkU0VoBUgnTkMFD0tNCvkoATA nTUEKcrLZsHDRcKxeF80rd/93wZZQuzhvzbfjh4aott702BNyZIHup3SsiNa7j7ZQQV8kvhsc OEyagIUo/PwDQOSipnA+ZUpcnU053L/FKSDgITrblxHwFYG090cFuo4Scy5xt2ssswHQXEyWB ktI0zTI6R0VMrXnj archived-at: Tue, 16 Aug 2016 14:11:36 -0000 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 > > >