简体   繁体   中英

[Scala]properly reading an object from a file in the presence of type erasure

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{
...
}

Have you looked into Manifest s?

There is a good topic on here that explores getting around type erasure already: How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?

Serializing any generic type in Java/Scala will suffer from type erasure.

There is a way to go around this using reflection and ClassTags (backed up with implicit conventions) but it assumes that the compiler has access to the initial types to be preserved. In the case of de-serializing an object from cold-storage, the compiler has no access to the initial type and it won't be able to help you.

Doing something similar to what you describe might be the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM