There’s a lot of talks about typelevel things in Scala. Shapeless has gained huge amount of attention lately. There are a lot of cool typelevel tricks out there, coined by Miles Sabin, the type astonaut. Some even say, he does not exist at the runtime. Anyway, I’d like to collect some of those tricks here, for future reference. I’m also doing this to get a better understanding of those tricks.
A way to guarantee that a polymorphic function will not return some specific type
Unboxed Tagged Types
There’s already a great explanation, including the needed intuition, in Eric Torreborre’s blog. This technique allows for having some typelevel guarantees on the values. Here’s a motivating example. Say, you want to create a function, which operates on positive doubles. Here’s how you could go about this:
It’s a totally ok way to do this, but imagine, you want several such functions, and you want to chain them afterwards. You might create something like
Let’s try that out:
But what would prevent someone from just
You would probably like to express your intents in types, if possible trying not to overuse single-valued case-classes and boxing/unboxing values. Here’s a possible solution:
Here positive is the so-called smart constructor. Let’s try it out. As before:
Here’s an implementation of this thing in scalaz. Scalaz uses this technique a lot, specifically for differentiating between different typeclass instances for the same type. For example, there are different semigroups for Int (default, under addition), Int @@ Multiplication, Int @@ MaxValue, Int @@ MinValue.
With the next scala version and some shapeless magic stuff like this is possible:
I was not able to push it to the level of functions though:
Enforcing that we don’t get a specified return type.
As pointed out by Miles, this works because the ambiguous implicits (nsubAmbig1 and nsubAmbig2) are more specific (specification).
I’ve recently answered one of the SO questions, using this technique. Here’s the implementation in scalaz repo. The trick allows for reducing the number of type annotations by guiding scala’s type inference.