groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Graeme Rocher (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (GROOVY-7957) Allow static compilation of builders that implement methodMissing / propertyMissing
Date Thu, 06 Oct 2016 10:03:20 GMT

    [ 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)

Mime
View raw message