[ 
https://issues.apache.org/jira/browse/GROOVY-10394?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Eric Milles updated GROOVY-10394:
---------------------------------
    Description: 
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.

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

  was:
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}}.


> 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.
> {{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