[Haskell-cafe] Overloading

MigMit miguelimo38 at yandex.ru
Sun Mar 10 10:01:46 CET 2013


On Mar 10, 2013, at 11:47 AM, Peter Caspers <pcaspers1973 at gmail.com> wrote:

> Thank you all for your answers, this helps a lot. To clarify my last point ...
> 
>>> Also again, taking this way I can not provide several constructors taking inputs of different types, can I ?
>> Sorry, didn't get what you mean here.
> 
> In C++ it is perfectly normal to have overloaded functions like
> 
> f : Int -> Int -> Int
> f : Int -> Char -> Int
> 
> in coexistence, because the compiler can infer (at compile time) what function to call by looking at the arguments types.
> 
> In Haskell I think this is not possible simply due to the flexibility given by partial function application, i.e.
> 
> f 5
> 
> would not be well defined any more, it could be Int -> Int or Char -> Int.

Well, that's what typeclasses are for.

class F a where f :: Int -> a -> Int

instance F Int where f = ...
instance F Char where f = ...

ghci> :t f 5
ghci> f 5 :: (F a) => a -> Int


More information about the Haskell-Cafe mailing list