Scala's Enumeration
class and the Enumeration.Val
readResolve
method do not appear to work as they should do (possibly related to this entry in Scala trac). Here is a program provided by *Steve Bendiola" to illustrate the problem:
@serializable
object InvestmentType extends Enumeration {
val Debt = Value("DEBT")
val Future = Value("FUTURE")
val Equity = Value("EQUITY")
}
Now a Main
class which can be run either with an arument W
where it will write out the enumeration values to a file, or R
where it will read them back in again:
object Main {
def main(args: Array[String]) = {
args(0) match {
case "R" => {
val ois = new ObjectInputStream(new FileInputStream("enum.ser"))
var obj: Object = null
foreach(ois) { obj =>
obj match {
case InvestmentType.Debt => println("got " + obj)
case InvestmentType.Equity => println("got " + obj)
case InvestmentType.Future => println("got " + obj)
case _ => println("unknown: " + obj + " of: " + obj.getClass)
}
}
}
case "W" => {
val oos = new ObjectOutputStream(new FileOutputStream("enum.ser"))
InvestmentType.foreach {i => oos.writeObject(i)}
oos.flush
oos.close
}
}
}
Both these methods require this foreach
method:
def foreach(os: ObjectInputStream)(f: Object => Unit) {
try {
val obj = os.readObject
if (obj != null) {
f(obj)
foreach(os)(f)
}
} catch {
case e: EOFException => //IGNORE
}
}
}
The output of this program should certainly not look like:
got DEBT
unknown: FUTURE of: class scala.Enumeration$Val
unknown: EQUITY of: class scala.Enumeration$Val
It appears to be related to which order the enumeration values are declared. Reordering them, it appears that the first value is always sensibly resolved