groovy

Allow sublists as lvalue in range notation

Details

  • Type: Wish Wish
  • Status: Closed Closed
  • Priority: Trivial Trivial
  • Resolution: Fixed
  • Affects Version/s: 1.0-JSR-1
  • Fix Version/s: 1.1-beta-2
  • Component/s: None
  • Labels:
    None
  • Environment:
    Groovy Version: 1.0-jsr-01 JVM: Blackdown-1.4.2-01
  • Number of attachments :
    0

Description

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

Activity

Hide
Paul King added a comment -

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.

Show
Paul King added a comment - 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.
Hide
Paul King added a comment -

No further feedback, assuming fixed

Show
Paul King added a comment - No further feedback, assuming fixed

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: