λ Tony's Blog λ
Partially Applying Scala type variablesPosted on September 22, 2008
Below is a neat trick that others may find useful when designing APIs using the Scala programming language.
Scala’s type inferencer is not as clever as some. As a result, we often find ourselves explicitly annotating type variables in certain contexts. This can become a little annoying, but what is worse, is if you have a type variable list and only one of those requires explicit specification, the others must also be explicitly specified even though they would have otherwise been inferred.
Let me demonstrate. I will use Scala’s higher-kinds, which are never inferred (this is quite acceptable given the problems associated with inferring higher-kinds – consider Java and C# where they do not even exist, thus leading to an enormous repetitive chore) though other examples may be applicable. However, it can be annoying if you were to write say, the Monad join function:
A caller invocation might look like:
Notice the explicit annotation with the
List type constructor, however, the second type variable (
Int) must also be explicitly specified, even though it could have otherwise been inferred. This is a bit annoying.
We can do away with it like so:
And now the caller code no longer has that clumsy type annotation:
Yay! Have a nice day :)