Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-890

Allow sublists as lvalue in range notation

    XMLWordPrintableJSON

Details

    • Wish
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • 1.0-JSR-1
    • 1.1-beta-2
    • None
    • None
    • 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

      Attachments

        Activity

          People

            paulk Paul King
            jot1109 Jochen Hinrichsen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: