[Haskell-cafe] Type classes, collections, sum types, closures, and a massive headache

Tim Docker tim at dockerz.net
Wed Jan 30 10:23:33 CET 2013

On 29/01/2013, at 12:43 PM, Bob Hutchison <hutch-lists at recursive.ca> wrote:

> The immediate problem is mapping an input to the system, some json message containing a reference to the 'thing' (like a key of some kind). I have to take that reference and find the thing and operate on it. All operations are easily accommodated by a type class. However, since I can't have a collection with mixed types even if the types satisfy a type class, I can't quite see how to actually store the things so I can find them.
> So there are a couple of obvious ways to handle this.
> I could use an ADT and a sum type of all the known kinds of thing, but I already know that this has to be extended and that's going to be problematic with users doing this on their own. And the type signatures look ugly. So I think that's not the best.
> I could use an ADT that contains functions that correspond to the functions of the type class, and that close over the 'thing' in question. I think this could be made to work, but I'm concerned with walking into more nasty surprises…

My advice is to go for the latter option. I'm not sure what nasty surprises you are expecting, but this "record of functions" approach is the one that I normally take when I am building a system that needs new types added without requiring global changes. I know that existentials and GADTs are possible solutions, but I've not needed the extra complexity here.



More information about the Haskell-Cafe mailing list