Type classes vs C++ overloading Re: [Haskell-cafe] Messing around with types [newbie]

Bulat Ziganshin bulat.ziganshin at gmail.com
Thu Jun 21 11:44:34 EDT 2007

Hello Dan,

Thursday, June 21, 2007, 7:39:35 PM, you wrote:

>>> class FooOp a b where
>>>   foo :: a -> b -> IO ()
>>> instance FooOp Int Double where
>>>   foo x y = putStrLn $ (show x) ++ " Double " ++ (show y)
>> this is rather typical question :)  unlike C++ which resolves any
>> overloading at COMPILE TIME, selecting among CURRENTLY available
>> overloaded definitions and complaining only when when this overloading
>> is ambiguous, type classes are the RUN-TIME overloading mechanism

> As I understood it, it was at COMPILE TIME (i.e. no type witness) 
> whenever explicitly type-annotated, implicitly when not exported from a
> module, or when inlined at the call site, at least in GHC. Or did I get
> this wrong?

overloading rules are general and they should work in any situation.
generally speaking, you define POLYMORPHIC function which will work
with any instance of FooOp class. there is no way to force GHC to use
ad-hoc overloading

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list