Type safe Scala โ Tagged types
๐
Today letโs talk about briefly about tagged types. A tagged type is useful when the type youโre working with is completely valid for your domain of application but still you want to add extra information to the type just as we did with phantom type parameters .
For example, letโs take the situation where we have to send information to some service, but the information needs to be protected with an ephemeral key. java.security.Key
is perfectly valid, but we can use tagged types to encode the fact that the key is ephemeral, and use that information whenever we need to check we are indeed using an ephemeral key:
The implementation for tagging types is really simple:
And the magic resides in that Tagged[T, U]
is a subtype of T : implicitly[Tagged[T, U] <:< T]
will yield a value for any T
and U
. Whenever we need T
, we can just use the tagged type variant, but if instead we need Tagged[T, U]
, we cannot pass T
without the required tag.
Take a look at how tagged types are implemented in shapeless , there they use @@
instead of Tagged and a class Tagger to benefit from type inference when tagging: tag Ephemeral instead of tag[Key, Ephemeral](key)
as it would be in our example if we couldnโt use the inference of the return type of our generateKey
function.