Another Tour of Scala

TypeSpecialization

The Gist

This isn’t covered by the tour.

My Interpretation

Type Specialization is a feature that allows the Scala compiler to create type-specific versions of classes that take type parameters. For example, if you have a collection of type Collection[T], and you specialize T, the compiler will create not just one class, but one for each primitive type as well as for the object type.

This is entirely transparent to the programmer, and essentially is useful for optimizing code that is being slowed down by autoboxing.

Note that the more type parameters you specialize, the more specialized classes are created, because each combination of primitives must be created to full specialize.

My Thoughts on This Feature

On one hand, this seems like a hyper-optimization that you should rarely really need. On the other hand, I can’t see a reason not to always do this, so why is it a feature at all? I suspect this is only useful for API writers who need to squeeze every ounce of performance out of their code.