Browse thread
[Caml-list] Restricting Method Overriding/Redefinition in Subclass
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| 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, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners