3 ms·
> If we draw a parallel to C++, Haskell's polymorphism is like templates It's important to note that even if Haskell's classes feel like C++ templates they are
by kevinnk 14y ago
> If we draw a parallel to C++, Haskell's polymorphism is like templates
It's important to note that even if Haskell's classes feel like C++ templates they are actually implemented more like abstract classes. In C++ you essentially create a new function for each template instantiation. In Haskell the function is passed an additional pointer telling the function how to implement the class. Kind of like a vtable pointer except not bound to the actual object. This has performance implications, so it's an important point to remember when comparing the languages.
- DanWaterworth 14y agoThat's a good point, however, the important aspect of typeclasses is that they're type-checked, not that they're compiled via monomorphization. Also, I believe that the way that typeclasses are compiled is at the discretion of the compiler, so actually typeclasses may be compiled in the same way as C++ templates. Another point is that monomorphisation isn't always possible, because a function could be run with an infinite number of types.
- fix_error 14y agoGHC is pretty good at specializing type-class-using functions for particular instances, e.g. if you have fac :: (Num a, Eq a) => a -> a fac 0 = 1 fac n = n*fac (n-1) main = do x <- readLn :: IO Int print (fac x) it will create a specialization (without any indirect calls) fac_Int :: Int -> Int and call that (in fact, in this case fac_Int will even be implemented by a worker function of type Int# -> Int# (unboxed ints)). If you don't want to rely on this automatic optimization, you can always add a pragma {-# SPECIALIZE fac :: Int -> Int #-}. (I used a recursive example because a non-recursive function would usually simply be inlined, avoiding any indirect calls too, assuming the call site is monomorphic).