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

ClassCastException when printing a mixed type int/long range

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.5.6, 1.6-beta-1
    • 1.5.7, 1.6-beta-2
    • None
    • None

    Description

      A mixed type int/long range throws a ClassCastException when printed.

      script.groovy
      def start  = Integer.MAX_VALUE
      def finish = Integer.MAX_VALUE + 2L
      def r = (start..finish)
      println r.getClass()  // class groovy.lang.ObjectRange
      println r.getFrom().getClass()  // class java.lang.Integer
      println r.getTo().getClass()  // class java.lang.Long
      
      println r.toString()  // 2147483647..2147483649
      println r  //java.lang.ClassCastException: java.lang.Integer
      
      println ('Range is ' + r.toString())  // Range is 2147483647..2147483649
      println ('Range is ' + r)  //java.lang.ClassCastException: java.lang.Integer
      

      BTW, I assume that converting the range to a list for printing happens on purpose:

      script.groovy
      def start  = 0
      def finish = 2
      def r = (start..finish)
      println r.toString()  // 0..2
      println r  // [0, 1, 2]
      

      Seems a bit strange to me, but well...

      Attachments

        Activity

          People

            paulk Paul King
            sirjective Christian Semrau
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: