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

Incorrect handling of final variables within switch

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.5.10, 3.0.2
    • Component/s: None
    • Labels:
      None

      Description

      The following code runs on groovy 2.4.18 but not on groovy 3.0.1.

      static void main(String[] args) {
      
          final String result
      
          switch (2) {
              case 1: result = "a"; break;
              case 2: result = "b"; break;
              default: result = "x"
          }
      
          println result
      }
      

      Error: Groovyc: The variable [result] is declared final but is reassigned.

      Since we use final in our codebase a lot (although it seems to be ignored by groovy 2.4),
      we will have to adjust a lot of places to make our code groovy 3.0 compatible.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                d.wilmer.1980 Daniel Wilmer
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 0.5h
                  0.5h