Java.lang.String cannot be translated into scala. Serializable

I am using scala. Serializable, but when I call String.asInstanceOf [Serializable], a cast class exception is thrown. Here is my code, quite simple.

arguments.map(_.asInstanceOf[Serializable]) 

Yes, the arguments are an array of strings, of course
I am running an application with scala -ide for eclipse with eclipse 3.7 and scala 2.9.0-1

view this scala doc documentation

Now the problem is that a typical use case is scala. Serializable "

+7
source share
2 answers

This is because java.lang.String does not implement scala.Serializable. There are no Java dependencies to scala.

I think you will find that you do not need any cast, since java.lang.String does implement java.io.Serializable, but perhaps the context you missed will make this clear.

+8
source

To add to what Ricky Clarkson said, it works

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

but it is not

 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 lines in Scala are of type java.lang.String ; they are native to the JVM and are unaware of Scala. According to the API docs , the Scala serializable trait exists for cross-platform compatibility (Java and .NET). If you're only on the JVM, then java.io.Serializable should be enough.

+9
source

All Articles