[Haskell-cafe] Overloading functions based on arguments?
duncan.coutts at worc.ox.ac.uk
Fri Feb 13 05:39:33 EST 2009
On Fri, 2009-02-13 at 13:25 +0300, Eugene Kirpichov wrote:
> class Foobar a b where
> foobar :: a -> b -> Int
> instance Foobar String Int where ...
> instance Foobar Int String where ...
But we typically do not to this. It's ugly. Classes work nicely when
there is some kind of parametrisation going on, where a function can
work with any instance of some interface. Ad-hoc overloading in the
style of Java/C++ just isn't done, even though it can be encoded by the
In the simple case just us a different name. If you would have lots of
variations then consider other approaches like passing a data type
containing some of the arguments (since that can encode alternatives).
> 2009/2/13 Daniel Kraft <d at domob.eu>:
> > Hi,
> > I just came across a problem like this: Suppose I've got two related
> > functions that do similar things, and I want to call them the same... Like
> > in:
> > foobar :: String -> Int -> Int
> > foobar :: Int -> String -> Int
> > (Bad example, but I hope you got the point.)
> > Is this kind of overloading (instead of the polymorphism based overloading)
> > possible in Haskell? Namely to have two functions with the same name but
> > different signatures so they could be distinguished by a call's parameters?
> > I fear not... So I guess I have to name the functions differently, right?
> > Thanks,
> > Daniel
More information about the Haskell-Cafe