[Haskell-cafe] Re: Newbie question

Stefan Monnier monnier at iro.umontreal.ca
Mon Jan 21 16:10:44 EST 2008

> How does caller choose which particular instance of Num they want?

By passing the type they want.  That's what the "Num a =>" thingy does.

> In object-oriented language If function return type is an interface it means
> that it can return any implementation of this interface, but caller can't
> choose which particular inplementation they want.

The full type of "f" you've given is:

  forall a . (Num a) => Integer -> a

where the "forall a ." is normally not written.  What you describe (a
function that returns something where the type can be chosen by the
function itself) would have type:

  Integer -> (exists a . (Num a) => a)

I.e. the "a" is not passed as a (type) argument, but instead it's
returned by the function.

> What the difference between haskell class and interface in object-oriented
> languge such Java or C#?

>From a low-level point of view, the difference is that the vtable is
manipulated separately from the objects.  The "Num a" basically stands
for the type of the vtable (which is called "dictionary" in Haskell).

To bundle an object with its vtable as is traditionally done in OO
languages, you need to create an existential package, e.g. something of
type (exists a . (Num a) => a).


More information about the Haskell-Cafe mailing list