I’d like to read a serialized object from a file in Scala, recovering functionality similar to Python’s pickle. My broken object reading code looks like this:
def ReadObjectFromFile[A](filename: String): A = {
val input = new ObjectInputStream(new FileInputStream(filename))
val obj = input.readObject()
obj match {
case a: A => a
case _ => sys.error("Type not what was expected when reading from file")
}
}
However, this code results in the warning “abstract type A in type pattern A is unchecked since it is eliminated by erasure”. What is the proper way to do this?
You can use implicits to get around type erasure as described in this blog post: