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 2021/11/28 17:23:00 UTC
[jira] [Resolved] (GROOVY-10394) SC: operands evaluated multiple times for <=> operator on reference types
[ https://issues.apache.org/jira/browse/GROOVY-10394?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles resolved GROOVY-10394.
----------------------------------
Resolution: Fixed
https://github.com/apache/groovy/commit/7a8f2e11d8e3c68ec90250a7555b7b236b85c86c
> SC: operands evaluated multiple times for <=> operator on reference types
> -------------------------------------------------------------------------
>
> Key: GROOVY-10394
> URL: https://issues.apache.org/jira/browse/GROOVY-10394
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Reporter: Eric Milles
> Assignee: Eric Milles
> Priority: Major
>
> Similar to GROOVY-7473, consider the following:
> {code:groovy}
> @groovy.transform.CompileStatic
> class C {
> int i = 0, j = 1
> Integer getA() { i++ }
> Integer getB() { j++ }
> void test() {
> assert (a <=> b) == -1
> print i
> print j
> }
> }
> new C().test()
> {code}
> In cases where "a" and "b" are not identical and non-null, each is evaluated 3 times (script prints "34" not "12"). This is not good if "a" and "b" are expensive to compute or have side effects.
> {{BinaryExpressionTransformer}} converts {{a <=> b}} into {{a === b ? 0 : (a == null ? -1 : (b == null ? 1 : a.compareTo(b)))}}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)