Last active
December 10, 2015 15:08
-
-
Save drdozer/4452329 to your computer and use it in GitHub Desktop.
Scala witness for serializable data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Witness that a type is safe to serialize. */ | |
@implicitNotFound(msg="Could not show ${S} to be serialization-safe") | |
trait SerializationSafe[S] | |
/** Serialization-safe witnesses. */ | |
object SerializationSafe { | |
/** If you are serializable, you are serialization-safe. */ | |
implicit def serializableIsSafe[S <: Serializable](s: S) = new SerializationSafe[S] {} | |
/** If you are a primitive, you are serialization-safe. */ | |
implicit def valuesAreSafe[A <: AnyVal] = new SerializationSafe[A] {} | |
} | |
... | |
// a function that can only be invoked on something that is serialization-safe | |
def [T : SerializationSafe] somethingThatRequiresSerializableData(t: T) = ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment