[Haskell-cafe] OO idioms redux

Graham Klyne GK at ninebynine.org
Wed Oct 13 04:17:09 EDT 2004

Haskell type classes don't really behave as one might expect coming from an 
OO perspective;  cf. 

That commentary doesn't say anything about interface inheritance.  I don't 
offhand have a good answer for that question.  In my own code, I guess I 
kind-of work around that issue.  As for inheritamnce of implementation, I 
guess that can be done "by hand", by building a new type that contains the 
"base" type.


At 18:32 12/10/04 +0000, John Goerzen wrote:
>OK, recently I posed a question about rethinking some OO idioms, and
>that spawned some useful discussion.
>I now have a followup question.
>One of the best features of OO programming is that of inheritance.  It
>can be used to slightly alter the behavior of objects without requiring
>modification to existing code or breaking compatibility with existing
>As an example, say I have a ConfigParser class.  This class can read in
>configuration files, provides various get/set methods to access them,
>and can write them back out.
>Now say I would like to make this a little more powerful.  Maybe I want
>to support the use of environment variables in my config file, so if
>there's a reference to $FOO in the file, it will be replaced by the
>contents of $FOO in the environment.
>In OO, I would make a new EnvConfigParser class.  I'd override the
>read() method.  My new read() would probably start by calling the
>parent's read() method, to get parsing for free.  Then it could iterate
>over the data, doing its environment variable substitution.
>Now, in Haskell, we obviously have no objects like this.  We do have
>something that provides some of the same benefits -- typeclasses.
>However, from what I can determine, they don't support algorithm
>inheritance like objects do in an OOP.  Specifically, it seems impossible to
>have two instances of a single typeclass that work on the same type,
>while having one share most of the code with the other.
>I'm wondering if there is a Haskell design pattern that I'm missing that
>would provid ethe kind of benefits that one gets from inheritance in the
>OO world.
>-- John
>John Goerzen
>Author, Foundations of Python Network Programming
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org

Graham Klyne
For email:

More information about the Haskell-Cafe mailing list