Scala Programming Language
  1. Scala Programming Language
  2. SI-3719

Enumeration.Val subclasses no-arg ctor breaks introspected naming

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Enumeration
    • Labels:
      None

      Description

      If subclassing Enumeration.Val to add behavior (methods), zero-param ctor doesn't supply null for name.

      Example code to support Mon.isWeekday has broken introspected element naming. The commented lines show the easy workaround, to supply the null name explicitly.

      (The no-arg ctor for Val has the result that nextName iterator supplies one null, and only the first element benefits from introspected naming; otherwise, the id is used.)

      Scala 2.8.0 final and confirmed on trunk, Java 6u16

      object Days extends Enumeration {
      	type Day = DayValue
      	val Mon, Tue, Wed, Thu, Fri, Sat, Sun = new DayValue // DayValue
      	
      	//private def DayValue: DayValue = new DayValue(nextId, if (nextName.hasNext) nextName.next else null)
      
          //protected class DayValue(i: Int, s: String) extends Val(i, s) {
      	protected class DayValue extends Val {
      		def isWeekday: Boolean = {
      			this match {
      				case Sun => false
      				case Sat => false
      				case _ => true
      			}
      		}
      	}
      }
      

      Test showing introspected naming:

      	//org.scalatest.junit.JUnitTestFailedError: Expected Array(Mon, Tue, Wed, Thu, Fri, Sat, Sun), but got Array(Mon, 1, 2, 3, 4, 5, 6)
      	@Test def dayElementsShouldBeNamed() {
      		expect(Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun")) {
      			Days.values.toList.map(x => x.toString).toArray
      		}
      	}
      	@Test def nameOfMon() {
      		import Days._
      		expect("Mon") {
      			val d: Day = Mon
      			d.toString
      		}
      	}
      	
      	//org.scalatest.junit.JUnitTestFailedError: Expected "[Tue]", but got "r1"
      	@Test def nameOfTue() {
      		import Days._
      		expect("Tue") {
      			val d: Day = Tue
      			d.toString
      		}
      	}
      

        Issue Links

          Activity

          Hide
          Lukas Rytz added a comment -

          for the person fixing the other enumeration bugs (SI-3687)

          Show
          Lukas Rytz added a comment - for the person fixing the other enumeration bugs ( SI-3687 )
          Hide
          Lukas Rytz added a comment -

          see also SI-3687

          Show
          Lukas Rytz added a comment - see also SI-3687
          Hide
          Hubert Plociniczak added a comment -

          (In r23893) Closes SI-3687, SI-3719, SI-3950, SI-3616. Plus removed some deprecated stuff for 2.9. Review by extempore

          Show
          Hubert Plociniczak added a comment - (In r23893) Closes SI-3687 , SI-3719 , SI-3950 , SI-3616 . Plus removed some deprecated stuff for 2.9. Review by extempore

            People

            • Assignee:
              Hubert Plociniczak
              Reporter:
              A. P. Marki
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development