views:

260

answers:

1

As far as I can tell, Scala has definitions for the Enumeration Value class for Value(Int), Value(String), and Value(Int, String).

Does anyone know of an example for creating a new Value subclass to support a different constructor?

For example, If I want to create an Enumeration with Value(Int, String, String) objects, how would I do it? I would like all of the other benefits of using the Enumeration class.

Thanks.

+4  A: 

The Enumeration values are instance of the Val class. This class can be extended and a factory method can be added.

object  My extends Enumeration{
    val A = Value("name", "x")
    val B = Value("other", "y")
    class MyVal(name: String, val x : String) extends Val(nextId, name)
    protected final def Value(name: String, x : String): MyVal = new MyVal(name,x)
}

scala> My.B.id
res0: Int = 1

scala> My.B.x
res1: String = y
Thomas Jung
Excellent! Works exactly like I wanted. Thanks.
Ralph