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

unexpected behavior with adding list to a number

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-beta-8
    • 1.0-beta-8
    • groovy-jdk
    • None
    • tested on Win2k

    Description

      just try

      print(5+[1,2])

      it results in null. I think that's nothing you would expect. But may be it's not a good idea to just make a new list [5,1,2]. So I suggest to throw an UnsupportedOperationException. This should always be the case when no operation is found, but as I'm new to the code of groovy and I didn't found the correct place to do this I will give only a fix for this special case:

      Index: groovy-core/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
      ===================================================================
      RCS file: /home/projects/groovy/scm/groovy/groovy-core/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java,v
      retrieving revision 1.122
      diff -u -r1.122 DefaultGroovyMethods.java
      — groovy-core/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java 5 Oct 2004 08:53:38 -0000 1.122
      +++ groovy-core/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java 4 Nov 2004 14:38:23 -0000
      @@ -1719,6 +1719,20 @@
      return answer;
      }

      +
      + /**
      + * Catch the unwanted case an Object and a Collection with the Collection on
      + * the right side.
      + *
      + * @param left an object
      + * @param right a Collection
      + * @return nothing
      + * @throws always an UnsupportedOperationException
      + */
      + public static List plus(Object left, Collection right)

      { + throw new UnsupportedOperationException("Left side has to be a list but was "+left.getClass().getName()); + }

      +
      /**

      • Create a List composed of the same elements repeated a certain number of times.
        *

      Attachments

        1. MethodFinder.patch
          1.0 kB
          Jochen Theodorou

        Activity

          People

            Unassigned Unassigned
            blackdrag Jochen Theodorou
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: