Sometimes, Haskell’s type system seems a bit restrictive compared to dynamic languages like Python. The most obvious example is the heterogenous list:
This is a contrived example, of course. But consider this use-case: I just want to print the content of the list. It’s unfortunate I can’t write:
For this specific application, the type system is overly restrictive – as long as all I want to do is print the content of my list! In this post, I’ll show you how to do something like this using the ExistentialQuantification language extension.
Let’s say I want to list American football players. There are two broad classes of players (offensive and defensive) and we want to keep track of the players in a list – the player registry. Our final objective is to print the list of players to standard output.
The Player type is too general; we’re not using the type system to its full potential. It’s much more representative of our situation to use two separate types: