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
          blackdrag blackdrag added a comment -

          Cedric, Guillaume and myself find all don't like this approach. While we want to be able to make Groovy programs we don't want it to become too much like Perl. the step from a=a?:2 to a=a?a:2 is small, but from a?:=2 to a=a?:2 I think is too much. Maybe with a different syntax - in another jira entry

          Show
          blackdrag blackdrag added a comment - Cedric, Guillaume and myself find all don't like this approach. While we want to be able to make Groovy programs we don't want it to become too much like Perl. the step from a=a?:2 to a=a?a:2 is small, but from a?:=2 to a=a?:2 I think is too much. Maybe with a different syntax - in another jira entry
          Hide
          Rodrigo Rosenfeld Rosas added a comment -

          The problem is that usually it is not "a = a ?: 2", but "someLongInstanceName.anotherLongInstancePropertyName = someLongInstanceName.anotherLongInstancePropertyName ?: 2". See what I mean?

          Show
          Rodrigo Rosenfeld Rosas added a comment - The problem is that usually it is not "a = a ?: 2", but "someLongInstanceName.anotherLongInstancePropertyName = someLongInstanceName.anotherLongInstancePropertyName ?: 2". See what I mean?
          Hide
          Matthias Hryniszak added a comment -

          I don't know if this is relevant in this case but

          def myMap = [:].withDefault {[]}
          

          would get rid of that lengthy construct with colon and questionmark:

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

          Which is actually shorter and a lot more readable than the funky syntax proposed here.

          Show
          Matthias Hryniszak added a comment - I don't know if this is relevant in this case but def myMap = [:].withDefault {[]} would get rid of that lengthy construct with colon and questionmark: if (condition) myMap[someLongNameObject.getKeyId()] << someNewValue Which is actually shorter and a lot more readable than the funky syntax proposed here.
          Hide
          Rodrigo Rosenfeld Rosas added a comment -

          Yes, this helps for this case, but I miss the "?:=" construction in several other cases as well.

          Show
          Rodrigo Rosenfeld Rosas added a comment - Yes, this helps for this case, but I miss the "?:=" construction in several other cases as well.
          Hide
          Rodrigo Rosenfeld Rosas added a comment -

          Mathias, please take a look at the other example in a prior comment.

          Show
          Rodrigo Rosenfeld Rosas added a comment - Mathias, please take a look at the other example in a prior comment.
          Hide
          Nathan Wells added a comment -

          note that in another thread in the grails forum, we decided to swap "?:=" with "?="

          I actually like the idea of a ?= operator. It's less about typing and more about readability. It's the difference between saying

          "assign 100 to params.max unless params.max is already set"
          params.max = params.max ?: 100

          and "params.max should default to 100"
          params.max ?= 100

          The only argument against that I've heard is that it adds complexity without any gain. I think there is substantial gain.

          Show
          Nathan Wells added a comment - note that in another thread in the grails forum, we decided to swap "?:=" with "?=" I actually like the idea of a ?= operator. It's less about typing and more about readability. It's the difference between saying "assign 100 to params.max unless params.max is already set" params.max = params.max ?: 100 and "params.max should default to 100" params.max ?= 100 The only argument against that I've heard is that it adds complexity without any gain. I think there is substantial gain.
          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: