[ https://issues.apache.org/jira/browse/GROOVY-7353?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14551661#comment-14551661 ]
ASF GitHub Bot commented on GROOVY-7353: ---------------------------------------- GitHub user paulk-asert opened a pull request: https://github.com/apache/incubator-groovy/pull/20 GROOVY-7353: Groovy should provide a MapConstructor AST transform You can merge this pull request into a Git repository by running: $ git pull https://github.com/paulk-asert/incubator-groovy groovy7353 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/incubator-groovy/pull/20.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #20 ---- commit bc4be4ddea107eacdcf4915f13e52bf6b4e7feb5 Author: Paul King <pa...@asert.com.au> Date: 2015-05-20T02:10:46Z GROOVY-7353: Groovy should provide a MapConstructor AST transform ---- > Groovy should provide a MapConstructor AST transform > ---------------------------------------------------- > > Key: GROOVY-7353 > URL: https://issues.apache.org/jira/browse/GROOVY-7353 > Project: Groovy > Issue Type: New Feature > Components: xforms > Affects Versions: 2.4.1 > Reporter: Paul King > Assignee: Paul King > > Groovy supports named-arg style constructor expressions if the object being > created contains a Map-based constructor (which currently must be created > explicitly) or a no-arg constructor plus appropriate setters. It would be > good to have an explicit MapConstructor transform which allowed automatic > creation of the Map-based constructor. This is useful for Java integration > purposes but also allows various configuration options to be set and better > support for final fields. > Such an AST transform would also potentially allow the Immutable AST > transform to be built from components (outside the scope of this issue). > The transform should work like this: > {code} > import groovy.transform.* > @TupleConstructor > class Person { > String first, last > } > @CompileStatic // optional > @ToString(includeSuperProperties=true) > @MapConstructor(pre={ super(args?.first, args?.last); args = args ?: [:] }, > post = { first = first?.toUpperCase() }) > class Author extends Person { > String bookName > } > assert new Author(first: 'Dierk', last: 'Koenig', bookName: > 'ReGinA').toString() == 'Author(ReGinA, DIERK, Koenig)' > assert new Author().toString() == 'Author(null, null, null)' > {code} > The generated map constructor has a single argument {{args}} of type {{Map}}. > In general, you should consider whether you want to guard against NPE > exceptions as the above example shows. -- This message was sent by Atlassian JIRA (v6.3.4#6332)