开发者

Why does scala.Serializable not specify any methods?

开发者 https://www.devze.com 2023-03-12 20:19 出处:网络
Because the Java language required all interface开发者_运维技巧 members to be public and the original designers didn\'t want to force the \"methods\" of java.io.Serializable to be public, this was not

Because the Java language required all interface开发者_运维技巧 members to be public and the original designers didn't want to force the "methods" of java.io.Serializable to be public, this was not possible in Java.

Scala doesn't have this restriction, but things like readObject/writeObject are still not specified in the scala.Serializable trait.

Wouldn't this help developers because

  • they had a guarantee that their signature is correct
  • it would make accessing these methods less akward.

or do I miss something important?


or do I miss something important?

Yes, you do.

readObject/writeObject methods have to be private and NOT overridden for the mechanism to work properly.

They are called in reverse way (i.e. superclass->class) too. Morealso you do wish that the method remains private to prevent misuse (and explicit calls)

Serializable mechanism offers other methods as well: like writeReplace+readResolve, that are usually not used in the same class + readObjectNoData (that's not so useful).

Now if you need specific method take a look at java.io.Externalizable. It does publish its methods and implementing it overrides the default serialization mechanism.


Answering from a Java background but I would guess the same reasoning applies to Scala: Java does not require methods marked Serializable to implement any methods; the runtime provides the serialization mechanism itself. That's why the interface is empty. readObject and writeObject are not part of Serializable not because they aren't public but because objects derived from Serializable don't need to implement them.

Serializable really shouldn't be an interface but an annotation (especially because a class derived from a Serializable class may very well not be Serializable itself), but it was part of the language before the language had annotations.


There are no methods to make public in serialization. There are four methods you may implement but they are all optional. Your question doesn't make sense.

0

精彩评论

暂无评论...
验证码 换一张
取 消