Issue Details (XML | Word | Printable)

Key: GROOVY-715
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Alex Tkachman
Reporter: Martin C. Martin
Votes: 2
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
groovy

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

Created: 01/Feb/05 10:45 AM   Updated: 12/Oct/08 08:49 AM
Component/s: None
Affects Version/s: 1.0-beta-9
Fix Version/s: 1.6-beta-2

Time Tracking:
Not Specified


 Description  « Hide
[-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.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Kim, Pilho added a comment - 06/May/05 08:03 PM
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 }


Martin C. Martin added a comment - 08/Feb/07 02:01 PM
That there's a workaround misses the point. As discussed on the user list on Feb. 8, 2007, this is worth considering.

Jochen Theodorou added a comment - 02/Jul/07 05:09 AM
we decided that at last in the 1.1 line statements do return null, only expressions can return something different