logoalt Hacker News

wk_end • yesterday at 4:29 PM • 1 reply • view on HN

So, for the sake of pedantry, in the programming language world "multiple dispatch" (and "[dynamic] dispatch" in general) typically refers to late binding - the function that's called is determined at runtime, and the caller doesn't actually need to know about the implementation being called.

What Haskell is doing there is basically just a kind of syntactic sugar around a pattern match, which is (to a very rough approximation) basically a super-powerful switch. Like a switch, the compress function knows about all of the branches statically, even if at runtime it dynamically selects which branch to take based on the structure of its arguments. And the structure of its arguments is "closed": it knows at compile-time all the different forms they can take.

Haskell achieves dynamic dispatch through typeclasses, and it can do multiple dispatch with the MultiParamTypeClasses extension. With these a caller doesn't need to know about the implementation it's invoking, and it doesn't need to know in advance anything about the possible values of its arguments beyond that they have an implementation that can be used - typeclasses are open, you can always define new implementations without updating every caller that uses them.


Replies

stingraycharles • yesterday at 4:44 PM

I agree with that, I tried to look for the correct term to describe this but the best thing I could find was multiple dispatch.

It's not just Haskell, though. Like I said, Erlang/Elixer also support this, and it enables writing some really elegant code.