Browse thread
[Caml-list] polymorphic methods
-
Damien
- brogoff@s...
-
Jacques Garrigue
-
Damien
- Jacques Garrigue
- Olivier Andrieu
-
Damien
[
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: | Olivier Andrieu <andrieu@i...> |
| Subject: | Re: [Caml-list] polymorphic methods |
Damien [Thursday 13 March 2003] : > My goal is to define a tree, whose node's type is a class > type a_t, containing a method for adding children. > but the child type can be any subtype of a_t > > * this method can safely be typed [a_t->unit], but this will require a > lot of coercions in the rest of the code... How about adding a coerce method in a_t ? That way, the coercion appears only in the class definition, not everytime you want to add a node. ,---- | class type a_t = object | method add : a_t -> unit | method children : a_t list | method coerce : a_t | end | | class a : a_t = | object (self) | val mutable children = [] | method add c = children <- c :: children | method children = children | method coerce = (self :> a_t) | end `---- and then : parent#add child#coerce or even : let adder p c = p#add c#coerce Hope this helps, -- Olivier ------------------- 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