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

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

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.8.1, 1.9-beta-1, 1.7.11
    • None
    • 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

              guillaume Guillaume Sauthier
              jcox Jon Cox
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: