[Haskell-cafe] C++ class = neutered (haskell class + haskell existential)

Bulat Ziganshin bulat.ziganshin at gmail.com
Sun Aug 20 03:31:16 EDT 2006


Hello Gabriel,

Sunday, August 20, 2006, 8:26:30 AM, you wrote:

> | There is a major difference though, in C++ (or java, or sather, or c#,
> | etc..) the dictionary is always attached to the value, the actual class
> | data type you pass around.

> The dictionary can be attached to the operations (not just to the values) by
> using objects local to functions (which sort of matierialize the
> dictionary).

This means that type classes can be _emulated_ in C++. but the bare
semantics is just as we said - in Haskell dictionaries are passed
around the functions (unless existential used) and in C++ they are
attached to objects

i agree that emulation you provided is closer to type classes at least
in that it implements _dynamic_ dispatching vs compile-time
dispatching of templates. but typical C++ programming involves using
classes and templates, not the unusual technique you have introduced.
please note that one can also use some other technique (say, function
pointers or switch statements) to emulate type classes behavior and
this don't means that switch statements in C is a kind of type classes :)

my comparison was targeted to teaching Haskell newbies (especially
former OOP programmers) how to use type classes, give them a
_intuition_ of this language feature and directions about its usage.
C++ templates with their type inference and dispatch based on the
type of entire operation is a best approximation in OOP world i know

but (of course) i will add your letter as one more description of that
type classes are, at this time - in terms of emulation them in C++ (in
addition to emulation them in Haskell, written by Wadler and Blott)


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



More information about the Haskell-Cafe mailing list