4 ms·
Haskell's type classes generalize further than multi methods do. You get dispatch based on any part of the type signature, not just the arguments.
by Peaker 14y ago
Haskell's type classes generalize further than multi methods do. You get dispatch based on any part of the type signature, not just the arguments.
- chongli 14y agoNot further than Clojure's multimethods. They dispatch on the value of an arbitrary function you write. This can be anything; it's not even restricted to types. Couple this with Clojure's ad hoc hierarchy system and even Haskell's types seem limited by comparison.
- Peaker 14y agoCan you implement the Read class in Clojure? class Read r where read :: String -> r (Let's ignore parse errors for our purposes here) Haskell supports return-type polymorphism. Clojure, being dynamically typed, cannot in general support this kind of overloading.
- chongli 14y agoGood point, though Clojure's extensible reader takes care of most of the use for this. I think it's a trade-off though. Can Haskell dispatch on a value rather than a type?
- Peaker 14y agoNote that this feature is far from being useful only for class Read. The Monad class actually depends on it too: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a The type of "return" can only be expressed with return-type polymorphism. This means you cannot really implement Haskell-like monads with dynamic typing. You have to implement them in a less general way. As for dispatching on runtime values, that is ordinary pattern matching: f (RuntimeValue1 x y) = ... case 1 ... f (RuntimeValue2 x y) = ... case 2 ...
- chongli 14y ago>This means you cannot really implement Haskell-like monads with dynamic typing. Ahh, yeah I've heard this before. It totally slipped my mind. >As for dispatching on runtime values, that is ordinary pattern matching Can you extend the patterns of a function at runtime (or even across modules)?
- Peaker 14y agoNo, you can't write a function spread across modules depending on the value of its input, short of implementing a dispatch system.