groovy
  1. groovy
  2. GROOVY-5291

Add "a ?:= 2" support: should be expanded to "a = a ?: 2"

    Details

    • Type: Improvement Improvement
    • Status: Reopened Reopened
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 4.0
    • Component/s: syntax
    • Labels:
      None
    • Number of attachments :
      0

      Description

      This would allow the very common useful idiom found in Ruby:

      variable ||= value
      

      Currently we have to write code like this in Groovy:

      def myMap = [:]
      
      if (condition) (myMap[someLongNameObject.getKeyId()] = myMap[someLongNameObject.getKeyId()] ?: []) << someNewValue
      

      Also, if getKeyId() is costly or shouldn't get called more than once for some reason, we would have to write it like:

      def myMap = [:]
      if (condition) {
        def key = someLongNameObject.getKeyId()
        (myMap[key] = myMap[key] ?: []) << someNewValue
      }
      

      When it could be simplified as this:

      if (condition) (myMap[someLongNameObject.getKeyId()] ?:= []) << someNewValue
      

        Issue Links

          Activity

          Hide
          Rodrigo Rosenfeld Rosas added a comment -

          Could you please take a look at this again as it has a different semantic from the other '?=' feature request?

          I'm concerned about consistency with the "?:" operator.

          Show
          Rodrigo Rosenfeld Rosas added a comment - Could you please take a look at this again as it has a different semantic from the other '?=' feature request? I'm concerned about consistency with the "?:" operator.
          Hide
          Matthias Hryniszak added a comment -

          Well... That's a whole new story now I have no idea if Guillaume and others will see the benefit but still. It does make the code more readable... And it's only 2 chars long which doesn't hurt my eyes...

          Show
          Matthias Hryniszak added a comment - Well... That's a whole new story now I have no idea if Guillaume and others will see the benefit but still. It does make the code more readable... And it's only 2 chars long which doesn't hurt my eyes...
          Hide
          Gavin Hogan added a comment -

          I gave this a vote for the form params.max ?= 100 which reads as "should default to".

          Show
          Gavin Hogan added a comment - I gave this a vote for the form params.max ?= 100 which reads as "should default to".
          Hide
          Nathan Wells added a comment -

          Rodrigo, can you link to the other ?= issue?

          Show
          Nathan Wells added a comment - Rodrigo, can you link to the other ?= issue?
          Hide
          Rodrigo Rosenfeld Rosas added a comment -

          I don't know if there is some special formatting tag for this, so I'll just paste the link:

          https://jira.codehaus.org/browse/GROOVY-5306

          Show
          Rodrigo Rosenfeld Rosas added a comment - I don't know if there is some special formatting tag for this, so I'll just paste the link: https://jira.codehaus.org/browse/GROOVY-5306

            People

            • Assignee:
              Unassigned
              Reporter:
              Rodrigo Rosenfeld Rosas
            • Votes:
              3 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: