You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/02/02 22:39:00 UTC

[jira] [Comment Edited] (GROOVY-10925) @TupleConstructor namedVariant breaks on setting private final fields in constructor

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

Eric Milles edited comment on GROOVY-10925 at 2/2/23 10:38 PM:
---------------------------------------------------------------

Not sure if it was intentional to provide the ability to fill in final properties via map constructor.  May have something to do with Immutable interaction.  [~paulk] Do you know of the history of Immutable, MapConstructor and TupleConstructor and final (read-only) properties?

This is the one test case that fails if MapConstructor skips final props:
{code:java}
    @Test
    void testPrivateFinalFieldAssignedViaConstructorShouldCauseError() {
        shouldFail shell, ReadOnlyPropertyException, '''
            @Immutable class Numbers {
                private final int b2 = -2
            }
            def n1 = new Numbers(b2:2)
        '''
    }
{code}


was (Author: emilles):
Not sure if it was intentional to provide the ability to fill in final properties via map constructor.  May have something to do with Immutable interaction.  [~paulk] Do you know of the history of Immutable, MapConstructor and TupleConstructor and final (read-only) properties?

> @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
>            Assignee: Eric Milles
>            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)