jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-5291

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

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Improvement Improvement
  • Status: Reopened Reopened
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: syntax
  • Labels:
    None

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

is related to

Improvement - An improvement or enhancement to an existing feature or task. GROOVY-5306 Add "a ?= 2" support: should be expanded to "a = a == null ? 2 : a"

  • Major - Major loss of function.
  • Open - The issue is open and ready for the assignee to start work on it.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
blackdrag blackdrag added a comment - 13/Feb/12 9:06 AM

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 - 13/Feb/12 9:06 AM 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
Permalink
Rodrigo Rosenfeld Rosas added a comment - 13/Feb/12 9:20 AM

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 - 13/Feb/12 9:20 AM The problem is that usually it is not "a = a ?: 2", but "someLongInstanceName.anotherLongInstancePropertyName = someLongInstanceName.anotherLongInstancePropertyName ?: 2". See what I mean?
Hide
Permalink
Matthias Hryniszak added a comment - 13/Feb/12 12:42 PM

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 - 13/Feb/12 12:42 PM 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
Permalink
Rodrigo Rosenfeld Rosas added a comment - 13/Feb/12 1:00 PM

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

Show
Rodrigo Rosenfeld Rosas added a comment - 13/Feb/12 1:00 PM Yes, this helps for this case, but I miss the "?:=" construction in several other cases as well.
Hide
Permalink
Rodrigo Rosenfeld Rosas added a comment - 13/Feb/12 1:01 PM

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

Show
Rodrigo Rosenfeld Rosas added a comment - 13/Feb/12 1:01 PM Mathias, please take a look at the other example in a prior comment.
Hide
Permalink
Nathan Wells added a comment - 15/Feb/12 1:26 PM

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 - 15/Feb/12 1:26 PM 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
Permalink
Rodrigo Rosenfeld Rosas added a comment - 15/Feb/12 2:27 PM

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 - 15/Feb/12 2:27 PM 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
Permalink
Matthias Hryniszak added a comment - 15/Feb/12 3:54 PM

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 - 15/Feb/12 3:54 PM 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
Permalink
Gavin Hogan added a comment - 15/Feb/12 7:45 PM

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

Show
Gavin Hogan added a comment - 15/Feb/12 7:45 PM I gave this a vote for the form params.max ?= 100 which reads as "should default to".
Hide
Permalink
Nathan Wells added a comment - 15/Feb/12 7:58 PM

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

Show
Nathan Wells added a comment - 15/Feb/12 7:58 PM Rodrigo, can you link to the other ?= issue?
Hide
Permalink
Rodrigo Rosenfeld Rosas added a comment - 16/Feb/12 4:34 AM

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 - 16/Feb/12 4:34 AM 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:
    blackdrag blackdrag
    Reporter:
    Rodrigo Rosenfeld Rosas
Vote (3)
Watch (3)

Dates

  • Created:
    09/Feb/12 5:13 AM
    Updated:
    16/Feb/12 4:47 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.