Sometimes I wonder whether people fully understand what a miracle it is that "tagged types" work at all. Even the parts of scala into which people have put design and implementation effort aren't going to win any robustness prizes at the county fair. In the case of tagged types... are there are any tests for it? Is the behaviour specified somewhere? Is there someone for whom it is a priority? These are real questions - maybe there's a little universe going which I don't see.
Undoubtedly, tagged types are a "little universe" right now. Perhaps 95% of Scala programmers are unaware of their existence. Here's how they ended up mattering alot to me...
Scala's something of a "broad church"; one partition is to divide it into a majority group for whom the main way to achieving data abstraction is by object-oriented techniques, i.e. dispatching on the run-time class, and a significant minority who see type class-based dispatch using implicits as the main game, and object-orientation as part of the legacy of Scala's origins and context(*).
As a Java guy, I started in the OO mainstream, but somewhere along the journey an impression that type classes are superior became overwhelming, and I stopped believing in OO. So my future, as I see it, lies in learning how to effectively re-purpose Scala as a type-class based language, and learning from other people who have made a similar realisation.
Type classes rely much more heavily on the type-system, and associating rich types with data is crucial to get dispatch to work right. My recent investigation of tagged types has been motivated by similar goals as the Value Classes SIP; that is, to get richer behaviour and polymorphism over primitive data types. Lets take stock of how the 2 approaches are going:
Tagged Primitive Types + Typeclasses:
- Can be processed without boxing
- Easy to reuse and compose operations over them without boxing effects
- Took a few people a few months to invent them by composing existing Scala features
- One major limitation is collision with innate operations defined over the native primitive type, which cannot be re-declared - exactly the problems of OO that type classes are trying to get away from.
Value Classes:
- Cannot be stored unboxed in arrays
- Cannot reuse implementation via traits without causing boxing
- Took a team a year to invent, compiler level changes needed, lots of implementation difficulties along the way, current result a compromise vs what was hoped for.
- Undoubtedly useful for optimising implicit wrappers. Further work seems needed, to be useful beyond that.
-Ben
(*) I'm interested Scala with type-classes, over Haskell, because:
- I want to be on the JVM and Java-compatible
- I want a strict not lazy language
- Good IDE tooling in the Java tradition matters to me