Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10394

SC: operands evaluated multiple times for <=> operator on reference types

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.0-rc-2
    • Static compilation
    • None

    Description

      Similar to GROOVY-7473, consider the following:

      @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()
      

      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)))

      Attachments

        Activity

          People

            emilles Eric Milles
            emilles Eric Milles
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: