You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Leonard Brünings (Jira)" <ji...@apache.org> on 2024/02/26 18:41:00 UTC
[jira] [Updated] (GROOVY-11325) @NamedVariant self referential default values are not correctly resolved when calculations are involved
[ https://issues.apache.org/jira/browse/GROOVY-11325?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Leonard Brünings updated GROOVY-11325:
--------------------------------------
Affects Version/s: 3.0.20
2.5.23
> @NamedVariant self referential default values are not correctly resolved when calculations are involved
> -------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-11325
> URL: https://issues.apache.org/jira/browse/GROOVY-11325
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.5.23, 3.0.20, 4.0.17
> Reporter: Leonard Brünings
> Priority: Major
> Labels: named-parameters
>
> This is a follow-up of GROOVY-10561. The fix didn't handle the case when the default value included any kind of calculation. Example in the [GroovyWebConsole|https://gwc-experiment.appspot.com/?g=groovy_4_0&codez=eNp9j8EKwjAMhu99ip9RmHNjLK2nQsEn8Oi9sk122Dq6UlDx3c1EUBEMgfDBlz9kGGcfIs7B-3SpY3DT0vsw1lshxP7gxq49ujC4KYq263Fy180wRSSCxU5VeIJiSFTSC_WKqqQCN2SSVcmGTDrDXbhl6fjcmpPIgJoC1iKnBkQglf8KFWcZqLeouOmPyFMb6K8F3eT8Dz7qAcyNP7Y]
> {code:groovy}
> @NamedVariant
> def baz(int v1 = 42, int v2 = v1+1, int v3 = v2+1) { "$v1 $v2 $v3" }assert baz() == '42 43 44'assert baz(10) == '10 11 12'assert baz(10, 20) == '10 20 21'assert baz(10, 20, 30) == '10 20 30'assert baz(v1: 10) == '10 11 12'assert baz(v1: 10, v2: 20) == '10 20 21'assert baz(v1: 10, v2: 20, v3: 30) == '10 20 30'
> {code}
> The generated code looks like this:
> {code:groovy}
> public java.lang.Object baz(@groovy.transform.NamedParams(value = [@groovy.transform.NamedParam(value = 'v1', type = int, required = false), @groovy.transform.NamedParam(value = 'v2', type = int, required = false), @groovy.transform.NamedParam(value = 'v3', type = int, required = false)]) java.util.Map namedArgs) {
> if (null == namedArgs ) {
> throw new java.lang.IllegalArgumentException('Named parameter map cannot be null')
> }
> for (java.lang.String namedArgKey : namedArgs.keySet()) {
> assert ['v1', 'v2', 'v3'].contains( namedArgKey ) : 'Unrecognized namedArgKey: ' + namedArgKey }
> return this.baz(namedArgs.containsKey('v1') ? namedArgs.v1 : 42, namedArgs.containsKey('v2') ? namedArgs.v2 : v1 + 1, namedArgs.containsKey('v3') ? namedArgs.v3 : v2 + 1)
> }
> {code}
> IMHO, it would be easiest to extract the map entries into local variables, then the references would be valid and would also work for cascading references.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)