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 2022/02/03 22:33:00 UTC
[jira] [Closed] (GROOVY-5746) Array assignment with operator does double call for index expression and array part
[ https://issues.apache.org/jira/browse/GROOVY-5746?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Paul King closed GROOVY-5746.
-----------------------------
> Array assignment with operator does double call for index expression and array part
> -----------------------------------------------------------------------------------
>
> Key: GROOVY-5746
> URL: https://issues.apache.org/jira/browse/GROOVY-5746
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 2.0.5, 2.3.0, 2.4.0-rc-1
> Reporter: Jochen Theodorou
> Assignee: Eric Milles
> Priority: Major
> Fix For: 4.0.0-beta-2
>
>
> {code:Java}
> @groovy.transform.CompileStatic
> class IndexTest {
> private int f = 0
> public int getIndex() {f++;0}
> def foo() {
> List<String> x = ["1","2"]
> x[index] += "1"
> assert x[0] == "11"
> assert f == 1
> }
> }
> def x = new IndexTest()
> x.foo()
> {code}
> The program above shows code in which we use an operator in combination with array assignment and an index with a side effect. This code must run without calling getIndex() twice. Since the field f is incremented twice, this is not the case. Normal Groovy executed the code as wished. The probable implementation is x[index] = x[index] + "1", which not only evals index twice, but also x. x and index must be evaluated only once!
--
This message was sent by Atlassian Jira
(v8.20.1#820001)