[
https://issues.apache.org/jira/browse/GROOVY-10925?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17684765#comment-17684765
]
Eric Milles commented on GROOVY-10925:
--------------------------------------
[~paulk] My initial thought is that this should not be supported for
{{@MapConstructor}}:
{code:groovy}
import groovy.transform.*
@MapConstructor
@ToString(includeNames=true)
class C {
final one = 1
def two = 2
}
def obj = new C(one:3, two:3)
def str = obj.toString()
assert str == 'C(one:3, two:3)'
{code}
I added test cases to demo the current behavior. Switching "MapConstructor" to
"TupleConstructor" in the above plus the change you have would add {{C(Object
one, Object two)}} and {{C(Object one)}} (not "two") which I think would be
unexpected.
Is there a sane path to remove what {{@MapConstructor}} is doing so that a
final field or property with an initializer -- probably need to check object
initializer blocks as well -- cannot be changed? The
{{ReadOnlyPropertyException}} that {{@Immutable}} produces is one option.
> @TupleConstructor namedVariant breaks on setting private final fields in
> constructor
> ------------------------------------------------------------------------------------
>
> Key: GROOVY-10925
> URL: https://issues.apache.org/jira/browse/GROOVY-10925
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.8
> Reporter: Joe Li
> Priority: Major
>
> @TupleConstructor `namedVariant` Breaks in Groovy4 when setting values to
> private final fields in constructor
>
> Example code in Groovy4 that throws the `Unrecognized namedArgKey: x` error:
> {code:java}
> import groovy.transform.*
> @TupleConstructor(includeFields = true, namedVariant = true)
> @ToString(includeNames = true, includeFields = true)
> class Foo {
> private final int x = 1
> private int y = 2
> private final int z
> }
> println new Foo(x:3, y: 3, z: 3) {code}
>
>
> After removing the `namedVariant` and add the `@MapConstructor` the code
> works:
> {code:java}
> import groovy.transform.*
> @MapConstructor(includeFields = true)
> @TupleConstructor(includeFields = true)
> @ToString(includeNames = true, includeFields = true)
> class Foo {
> private final int x = 1
> private int y = 2
> private final int z
> }
> println new Foo(x:3, y: 3, z: 3) {code}
>
>
> Related issue:
> [GROOVY-10919|https://issues.apache.org/jira/browse/GROOVY-10919],
> @MapConstructor Breaks in Groovy4 when combined with @TupleConstructor, fine
> in Groovy3
--
This message was sent by Atlassian Jira
(v8.20.10#820010)