You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2015/05/20 04:24:02 UTC

[jira] [Comment Edited] (GROOVY-7353) Groovy should provide a MapConstructor AST transform

    [ https://issues.apache.org/jira/browse/GROOVY-7353?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14477031#comment-14477031 ] 

Paul King edited comment on GROOVY-7353 at 5/20/15 2:23 AM:
------------------------------------------------------------

-Initial PR. Still requires doco and tests.- MOVED
https://github.com/groovy/groovy-core/pull/644


was (Author: paulk):
Initial PR. Still requires doco and tests.
https://github.com/groovy/groovy-core/pull/644

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