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 2017/05/02 02:03:05 UTC
[jira] [Closed] (GROOVY-7353) Groovy should provide a
MapConstructor AST transform
[ https://issues.apache.org/jira/browse/GROOVY-7353?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Paul King closed GROOVY-7353.
-----------------------------
> 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
> Fix For: 2.5.0-alpha-1
>
>
> 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.15#6346)