groovy

Optional return for "if" statements [minor breaking change]

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.0-beta-9
  • Fix Version/s: 1.6-beta-2
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

[-2, -6, 5].collect { if (it < 0) -it; else it }

returns "[null, null, null]," because the parser considers the last statement to be an "if" statement. Instead, we can propagate the optional return to the if and else clauses, so that the above will return [2, 6, 5].

James feels it should only be done for New Groovy.

Activity

Hide
Kim, Pilho added a comment -

Since jsr-01, we can use the keyword "return" in closures.

Here is a workaround for jsr-01:
[-2, -6, 5].collect { if (it < 0) return -it; else return it }

Show
Kim, Pilho added a comment - Since jsr-01, we can use the keyword "return" in closures. Here is a workaround for jsr-01: [-2, -6, 5].collect { if (it < 0) return -it; else return it }
Hide
Martin C. Martin added a comment -

That there's a workaround misses the point. As discussed on the user list on Feb. 8, 2007, this is worth considering.

Show
Martin C. Martin added a comment - That there's a workaround misses the point. As discussed on the user list on Feb. 8, 2007, this is worth considering.
Hide
blackdrag blackdrag added a comment -

we decided that at last in the 1.1 line statements do return null, only expressions can return something different

Show
blackdrag blackdrag added a comment - we decided that at last in the 1.1 line statements do return null, only expressions can return something different

People

Vote (2)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: