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

Invalid behaviour of ?: operator within an 'if' statement in closure

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.0.0
    • 2.0.1
    • ast builder, Compiler
    • OpenSUSE 11.4 x86_64, Oracle Java SE 1.6 Update 29

    Description

      The invalid result appears only in 2.0.0 version of Groovy:

      dm@dmc:~/bin/groovy-2.0.0/bin> ./groovysh
      Groovy Shell (2.0.0, JVM: 1.6.0_29)
      Type 'help' or '\h' for help.
      ---------------------------------------------
      groovy:000> def c =

      {def s; if (it.y == null) s = true; else s = it.z < it.x - 1 ? null : true; s}

      ; c(z: 0, y: 2, x: 3)
      ===> false
      groovy:000>

      In Groovy 1.8.6 the result is correct.

      dm@dmc:~/bin/groovy-1.8.6/bin> ./groovysh
      Groovy Shell (1.8.6, JVM: 1.6.0_29)
      Type 'help' or '\h' for help.
      ----------------------------------------------
      groovy:000> def c =

      {def s; if (it.y == null) s = true; else s = it.z < it.x - 1 ? null : true; s}

      ; c(z: 0, y: 2, x: 3)
      ===> null
      groovy:000>

      It can be fixed by replacing the ternary operator with 'else if' and 'else'.

      Attachments

        1. testCase.groovy
          0.1 kB
          Dmitry Ovchinnikov

        Activity

          People

            melix Cédric Champeau
            dmovchinn Dmitry Ovchinnikov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: