Browse thread
[Caml-list] examples of heterogenous collections (containers ?)
[
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] examples of heterogenous collections (containers ?) |
On Wed, 2004-03-31 at 16:45, briand@aracnet.com wrote: > So it seems to me that I need a "stronger" method to enforce safety, > perhaps implementing x1 as a virtual method in base ? You are thinking about this problem *backwards*! :) Forget about how to declare the classes etc. Focus on the list. You would like a list of some type "elt" for which you can write: List.iter (fun x -> x#f) the_list; Now define the type which 'the_list' must have. Define the 'minimal' such type! class type elt = object method f : unit end val the_list : elt list That's it. All you need to do now is cast ANY object to type "elt" and it will fit into your list. The cast will always work if the object has a method named 'f' which when invoked with no arguments returns unit. Obviously, because Ocaml is wonderful, your cast will fail on objects without a suitable 'f' method. The KEY idea here is: the class types you need for algorithms should be declared. But forget the idea classes have types. They do, but it isn't important. A class is just a function which creates an object. Class types are *views* of objects. The class type of a class is the 'maximal' view of an object which it constructs, and isn't really that important, in fact, it should probably never be exposed in any interfaces because the full class type will include data members due to a problem needing to allocate space for them when constructing an object. The solution is to use a factory function in a single module, and return an abstraction of the class type you wish to deal with. -- 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