Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.String cannot be cast to scala.Serializable

I am using scala.Serializable, but when i invoke String.asInstanceOf[Serializable], class cast exception is thrown. Here is my code, pretty simple.

arguments.map(_.asInstanceOf[Serializable])

Yes, arguments is array of string of course
I am run the application with scala-ide for eclipse with eclipse 3.7 and scala 2.9.0-1

looking into this documentation scala doc

The Problem now is "what is the typical use case of scala.Serializable"

like image 637
jilen Avatar asked Aug 16 '11 03:08

jilen


1 Answers

To add to what Ricky Clarkson said, this works

scala> "hi".asInstanceOf[java.io.Serializable]
res7: java.io.Serializable = hi

but this doesn't,

scala> "hi".asInstanceOf[scala.Serializable]
java.lang.ClassCastException: java.lang.String cannot be cast to scala.Serializable
...

Without qualification, Serializable in Scala refers to scala.Serializable. Note that strings in Scala are of type java.lang.String; they are "native" to the JVM and don't know about Scala. According to the API docs, the Scala serializable trait exists for cross platform compatibility (Java and .NET). If you're just on the JVM, then java.io.Serializable should be sufficient.

like image 122
Kipton Barros Avatar answered Sep 28 '22 17:09

Kipton Barros