Issue Details (XML | Word | Printable)

Key: GROOVY-890
Type: Wish Wish
Status: Closed Closed
Resolution: Fixed
Priority: Trivial Trivial
Assignee: Paul King
Reporter: Jochen Hinrichsen
Votes: 0
Watchers: 2
Operations

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

Allow sublists as lvalue in range notation

Created: 15/Jun/05 12:27 PM   Updated: 26/Sep/07 08:32 PM
Component/s: None
Affects Version/s: 1.0-JSR-1
Fix Version/s: 1.1-beta-2

Time Tracking:
Not Specified

Environment: Groovy Version: 1.0-jsr-01 JVM: Blackdown-1.4.2-01


 Description  « Hide
In my opinion it would be nice to allow for lvalue slicing using the range operator. Example: reverse the first n elements of a list

// throws IllegalArgumentException
list[0..<n] = list[0..<n].reverse()

The exception is thrown in DefaultGroovyMethods.java ("You must specify a list of 2 indexes to create a sub-list") for all n != 2 (i.e. almost always)

Rewriting the statement as

list[0, n-1] = list[0..<n].reverse()

works but is not as nice.

It might be enough to change DefaultGroovyMethods.java from

if (splice.size() != 2) { throw new IllegalArgumentException("You must specify a list of 2 indexes to create a sub-list"); }
int left = InvokerHelper.asInt(splice.get(0));
int right = InvokerHelper.asInt(splice.get(1));

to

int left = InvokerHelper.asInt(splice.get(0));
int right = InvokerHelper.asInt(splice.get(splice.length()));

but i'm not sure what other parts are affected 'cause i'm a complete newbie

Thanks

Jochen



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Paul King added a comment - 11/Jun/07 04:08 AM
In HEAD, this now behaves as follows:
def list = [0, 1, 2, 3, 4, 5]
list[1..<4] = list[1..<4].reverse()
println list

// => [0, 3, 2, 1, 4, 5]

Which I believe is the requested behaviour.


Paul King added a comment - 26/Sep/07 08:32 PM
No further feedback, assuming fixed