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 33A14200B96 for ; Thu, 6 Oct 2016 12:03:22 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 324A6160AE0; Thu, 6 Oct 2016 10:03:22 +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 73B61160AAD for ; Thu, 6 Oct 2016 12:03:21 +0200 (CEST) Received: (qmail 11408 invoked by uid 500); 6 Oct 2016 10:03:20 -0000 Mailing-List: contact notifications-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 notifications@groovy.apache.org Received: (qmail 11397 invoked by uid 99); 6 Oct 2016 10:03:20 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Oct 2016 10:03:20 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id 6A9CC2C0087 for ; Thu, 6 Oct 2016 10:03:20 +0000 (UTC) Date: Thu, 6 Oct 2016 10:03:20 +0000 (UTC) From: "Graeme Rocher (JIRA)" To: notifications@groovy.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Comment Edited] (GROOVY-7957) Allow static compilation of builders that implement methodMissing / propertyMissing MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Thu, 06 Oct 2016 10:03:22 -0000 [ https://issues.apache.org/jira/browse/GROOVY-7957?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15551516#comment-15551516 ] Graeme Rocher edited comment on GROOVY-7957 at 10/6/16 10:02 AM: ----------------------------------------------------------------- Some discussion about this has happened on the Groovy developer list. The following items were discussed and agreed: The {{methodMissing}} and {{propertyMissing}} methods should be able to have multiple overloaded signatures with types and a marker interface should be used. Suggested name is {{AutoDelegationBuilder}} (suggestions welcome): {code} class MarkupBuilder implements AutoDelegationBuilder { MarkupNode methodMissing(String name, Map attributes, Closure body) { ... } MarkupNode methodMissing(String name, Closure body) { ... } MarkupNode methodMissing(String name) { ... } } {code} Where possible when using {{@CompileStatic}} a concrete method should be chosen so: {code} class MarkupBuilder implements AutoDelegationBuilder { MarkupNode methodMissing(String name, @DelegatesTo(MarkupBuilder) Closure body) { ... } MarkupNode body(Closure body) { ... } } def mkp = new MarkupBuilder() mkp.html { body { // invoke the body method directly } } {code} The {{body}} method should be invoked directly without going through {{Closure}} resolution logic, when a method is not found {{methodMissing}} should be invoked directly. The value of {{DelegatesTo}} should be respected, otherwise invocation should happen against the original builder. was (Author: graemerocher1): Some discussion about this has happened on the Groovy developer list. The following items were discussed and agreed: The {{methodMissing}} and {{propertyMissing}} methods should be able to have multiple overloaded signatures with types and a marker interface should be used. Suggested name is {{AutoDelegationBuilder}} (suggestions welcome): {code} class MarkupBuilder implements AutoDelegationBuilder { MarkupNode methodMissing(String name, Map attributes, Closure body) { ... } MarkupNode methodMissing(String name, Closure body) { ... } MarkupNode methodMissing(String name) { ... } } {code} Where possible when using {{@CompileStatic}} a concrete method should be chosen so: {code} class MarkupBuilder { MarkupNode methodMissing(String name, @DelegatesTo(MarkupBuilder) Closure body) { ... } MarkupNode body(Closure body) { ... } } def mkp = new MarkupBuilder() mkp.html { body { // invoke the body method directly } } {code} The {{body}} method should be invoked directly without going through {{Closure}} resolution logic, when a method is not found {{methodMissing}} should be invoked directly. The value of {{DelegatesTo}} should be respected, otherwise invocation should happen against the original builder. > Allow static compilation of builders that implement methodMissing / propertyMissing > ----------------------------------------------------------------------------------- > > Key: GROOVY-7957 > URL: https://issues.apache.org/jira/browse/GROOVY-7957 > Project: Groovy > Issue Type: New Feature > Components: GEP > Reporter: Graeme Rocher > > Similar to Scala's {{Dynamic}} http://www.scala-lang.org/api/current/index.html#scala.Dynamic > We should be able to write builders that are usable from statically compiled code. To achieve this I recommend the following: > {code} > // if the object implements methodMissing dispatch to it > foo.bar("blah") ~~> foo.methodMissing("bar", "blah") > // if the object implements propertyMissing dispatch to it > foo.var1 ~~> foo.propertyMissing("var1") > // if the object implements propertyMissing dispatch to it > foo.var1 = 10 ~~> foo.propertyMissing("var1", 10) > {code} > When combined with GROOVY-7956 it would then be possibly to fully implement builders that are compatible with {{@CompileStatic}} and certain existing builders could be enhanced to take advantage of this feature {{JsonBuilder}}, {{MarkupBuilder}} etc. -- This message was sent by Atlassian JIRA (v6.3.4#6332)