Is there a way to provide a Serializable type at compile time

I often work with Spark, and it will save me a lot of time if the compiler can guarantee that the type is serialized.

Perhaps with a type class?

def foo[T: IsSerializable](t: T) = { // do stuff requiring T to be serializable } 

This is not enough to limit T <: Serializable. It can still fail at runtime. Unit tests are a good replacement, but you can still forget them, especially when working with large teams.

I think this cannot be done at compile time without sealed types.

+6
source share
1 answer

Yes, perhaps, but not in the way you hope. The type class IsSerializable can provide a mechanism for converting your T to a type value guaranteed by Serializable and vice versa.

 trait IsSerializable[T] { def toSerializable(t: T): String def fromSerializable(s: String): Option[T] } 

But, of course, it's just an alternative serialization mechanism based on a class of classes, which in itself, which makes the use of JVM serialization redundant.

Your best course of action has been to lobby Spark to directly support class-based serialization.

+4
source

All Articles