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

Ternary operator in a property assignment

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Information Provided
    • 2.4.18
    • None
    • None

    Description

      Why ternary operator in properties works incorrectly with strings?

      class Parent {
          def String name = (name == null || name == 'somebadname') ? 'default' : name
      }
      
      a = new Parent()
      println("name is: ${a.name}")
      
      b = new Parent(name: 'somebadname')
      println("name is: ${b.name}")
      
      c = new Parent(name: 'other')
      println("name is: ${c.name}")
      
      

      output:

      name is: default
      name is: somebadname
      name is: other
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            gekm gekm
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: