Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumeration.Val subclasses no-arg ctor breaks introspected naming #3719

Closed
scabug opened this issue Aug 1, 2010 · 4 comments
Closed

Enumeration.Val subclasses no-arg ctor breaks introspected naming #3719

scabug opened this issue Aug 1, 2010 · 4 comments
Assignees
Labels

Comments

@scabug
Copy link

scabug commented Aug 1, 2010

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
		}
	}
@scabug
Copy link
Author

scabug commented Aug 1, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3719?orig=1
Reporter: @som-snytt
See #5211

@scabug
Copy link
Author

scabug commented Aug 9, 2010

@lrytz said:
for the person fixing the other enumeration bugs (#3687)

@scabug
Copy link
Author

scabug commented Aug 10, 2010

@lrytz said:
see also #3687

@scabug
Copy link
Author

scabug commented Jan 4, 2011

@hubertp said:
(In r23893) Closes #3687, #3719, #3950, #3616. Plus removed some deprecated stuff for 2.9. Review by extempore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants