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

Ternary operator ?: does not handle newline before ":" gracefully

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.8.1, 1.9-beta-1, 1.7.11
    • Component/s: None
    • Labels:
      None

      Description

      It seems like Groovy should be able to handle "Case 5" below,
      but instead it reports a compile-time error.

      //-----------------------
      //      Case 1:  OK
      //-----------------------
      bar =  0 ?  "moo" : "cow"
      
      
      //-----------------------
      //      Case 2:  OK
      //-----------------------
      bar =  0 ?
             "moo" : "cow"
      
      //-----------------------
      //      Case 3:  OK
      //-----------------------
      bar =  0 ?  "moo" :
             "cow"
      
      //-----------------------
      //      Case 4:  OK
      //-----------------------
      bar =  0 ?
             "moo" :
             "cow"
      
      //---------------------------------------------------
      //      Case 5:  ERROR.
      //
      //      Compiler says:
      //              expecting ':', found '<newline>'
      //---------------------------------------------------
      bar =  0 ? "moo"
               : "cow"
      
      
      //-----------------------------------------------------
      //      Case 6:  OK
      //
      //          Groovy cannot figure out that this is a
      //          valid ternary without the help of the '\'
      //          line continuation escape.  How come?
      //
      //-----------------------------------------------------
      bar =  0 ? "moo"        \
               : "cow"
      
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Guillaume Guillaume Delcroix
                Reporter:
                jcox Jon Cox
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: