Groovy
  1. Groovy
  2. GROOVY-890

Allow sublists as lvalue in range notation

    Details

    • Type: Wish Wish
    • Status: 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

      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

        Jochen Hinrichsen created issue -
        Paul King made changes -
        Field Original Value New Value
        Assignee Paul King [ paulk ]
        Paul King made changes -
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.1-beta-2 [ 10436 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12730040 ] Default workflow, editable Closed status [ 12742015 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12968514 ] Default workflow, editable Closed status [ 12976309 ]

          People

          • Assignee:
            Paul King
            Reporter:
            Jochen Hinrichsen
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development