Eric Milles created GROOVY-10394:
------------------------------------

             Summary: 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


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.  This is not good if "a" and "b" are expensive to compute or have side 
effects.

{{a <=> b}} is transformed into {{a === b ? 0 : (a == null ? -1 : (b == null ? 
1 : a.compareTo(b)))}} by {{BinaryExpressionTransformer}}.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to