Skip to content

Instantly share code, notes, and snippets.

@drdozer
Last active December 10, 2015 15:08
Show Gist options
  • Save drdozer/4452329 to your computer and use it in GitHub Desktop.
Save drdozer/4452329 to your computer and use it in GitHub Desktop.
Scala witness for serializable data
/** 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