Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Restricting Method Overriding/Redefinition in Subclass
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-08-14 (07:39)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Restricting Method Overriding/Redefinition in Subclass
On Sat, 2004-08-14 at 10:03, chris.danx wrote:

> Perhaps a radically different solution is best?  I chose an OO solution 
> so that new classes could be trivially added to the system.

There is another kind of solution you might investigate.

Instead of trying to unify all your data types using 
abstraction -- classes provide abstraction --
use summation instead.

A summation solution looks like:

type obj = [
  | `A of a 
  | `B of b * obj 
  | `C of c * obj * obj

using polymorphic variants. In order to 
'do something' with each object, including
children, you write a 'visitor' style function:

let rec iter x = match x with
  | `A  a -> do_a a
  | `B (b ,kid) -> do_b b; iter kid
  | `C (c, k1, k2) -> iter k1; do_c c; iter k2

When you need to add a new object type to this
system you have to add the method for handling
it, just as in the OO solution.

The difference is that the method doesn't go
in with the data lexically, but separately
in the visitor function. Ocaml will make sure
you don't forget.

This kind of solution is more flexible than the OO
style solution, since you can write many different
kinds of 'visitor' style functions without invading
your data descriptions. The downside is that the
code for each kind is scattered through the program,
whereas with classes its localised lexically.

Which solution is best depends on how homogenous
your object kinds are. If they're all instances
of a single abstraction -- use classes.

If they're heterogenous objects -- use summation.

You can of course mix both solutions together -- abstract
what really is abstract, and unify what is not 
using variants.

John Skaller,
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: