Closed
Description
Minimized code
Running twice with 0.25.0-RC1:
import java.io._
enum A:
case B
object Main extends App:
val file = File("/tmp/EnumSerialization.dat")
if file.exists
val a = ObjectInputStream(FileInputStream(file)).readObject().asInstanceOf[A]
println(a == A.B)
a match
case A.B => // passes exhaustivity check
else
ObjectOutputStream(FileOutputStream(file)).writeObject(A.B)
Output
On second execution:
false
[error] java.lang.ExceptionInInitializerError
...
[error] Caused by: scala.MatchError: B (of class A$$anon$1)
Expectation
I expected a == A.B
because it is marked Serializable
by default.
I think there are two options:
- to stop
Enum
(with singleton cases) extendingSerializable
, which seems to go against Mirror.Product is available for enum values that are not subtypes of Product #9011, or - to generate custom
writeObject()
andreadObject()
when desugaring enum, though I'm not sure if it's possible and worthwhile.