Let's say I have a map stored on disk and I should like to retrieve it:
type myType = Map[something , somethingElse]
...
try{
val bytes = Files.readAllBytes(path)
val is = new ObjectInputStream(new ByteArrayInputStream(bytes))
val m = is.readObject().asInstanceOf[myType]
Some(m)
}catch{
case _:FileNotFoundException | _:IOException | _:ClassCastException => None
}
So far so good. However, as Maps are generic and due to the ever-annoying type erasure, I doubt I can conveniently rely on the ClassCastException
to make sure that if I ever change myType
, outdated maps will be discarded.
The thought has crossed my mind to simply hash myType
and to retrieve and compare the hash prior to retrieving the map, but that feels more like a workaround than a solution. What would be the proper way to handle this?
Edit: The maps were stored to disk as follows:
var myMap : myType = ...
...
try{
val b = new ByteArrayOutputStream()
val os = new ObjectOutputStream(b)
os.writeObject(myMap)
Files.write(path, b.toByteArray)
}catch{
...
}