4 ms·
I don't see how Haskell typeclass methods are any different from C++ overloaded functions in this regard. In both languages you refer to a concrete function by
by chancho 18y ago
I don't see how Haskell typeclass methods are any different from C++ overloaded functions in this regard. In both languages you refer to a concrete function by its signature rather than its name. Eg in Haskell you'd have "(+) :: Int -> Int -> Int" or "(+) :: Float -> Float -> Float". You have this additional way to refer to the abstract version, "Num a => (+) :: a -> a -> a" but I don't see how that helps in debugging, other than to simultaneously set breakpoints on all the concrete functions which constitute the abstract method. Also C++0x concepts bring these two frameworks closer together.
On the larger issue of free-for-all overloading I mostly agree with you: it causes unnecessary confusion. I'm comfortable with it and like it in my own personal code but I could see how you would want to avoid it in larger projects.
- SamReidHughes_ 18y agoC++ requires that all overloaded versions exist at compile time; Haskell allows them to exist at runtime. For example, in Haskell you can make a datatype like data Foo a = Blah a | Bar (Foo [a]) and you'll have no trouble making a function "showFoo :: Show a => Foo a -> String" using straightforward programming. If you try to do that in C++ you'll get infinite template recursion. Also, C++ templates don't restrict you to match the right type signatures, the way Haskell typeclasses do. But other than that, they are pretty much the same kind of thing and are used in the same way.
- chancho 18y agoYou've mixed together a lot of stuff here. your Foo data type is recursive. I can achieve something similar in C++ without infinite template recursion, but I have to use pointers to do it. (Your Haskell has pointers too, they're just hidden from you.) Also there is nothing 'runtime' about the example you've got here. I can do the same thing with a tagged-union type in C/C++ and a switch statement, which is what your Haskell compiles down to. Maybe you were thinking of existential types? http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types http://en.wikibooks.org/wiki/Haskell/Existentially_quantifie... . That's something C++ can't do. --------- Edit: nm I see what you did there with the Foo a = ... | Blah (Foo [a]). That would probably break C++.
- jmillikin 18y agoIn Haskell, you can't have `(+) :: String -> String -> String`, or `(+) :: Float -> Int -> Maybe String`. Maybe I chose a poor phrasing -- I meant that, when debugging, in Haskell it's easier to figure out which function implements the operator. In C++, I usually have to resort to a debugger.