Browse thread
[Caml-list] troubles with polymorphic variant in class
[
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: | 2004-06-13 (18:00) |
From: | skaller <skaller@u...> |
Subject: | Re: [Caml-list] troubles with polymorphic variant in class |
On Mon, 2004-06-14 at 02:05, François-Xavier HOUARD wrote: > The method add_child has type ([> `Whatever ] as 'a) -> unit where 'a > is unbound > _Is there another way to make it ??? The notation [> `X] does not denote a type, but a family of types (all types containing variant `X). Alternatively you might say it is a type constraint. But a list must have elements of a definite type. You can define the type as for ordinary variants: type widget = [ `Whatever | `Window of window | `Button of button] and now make a widget list. Unfortunately, you cannot add child widgets to a button like this: class button = object val mutable (childs:widget list) method add_child x = childs <- x::childs end because button isn't defined yet .. and neither is window... so you can't define widget. Unfortunately the obvious way to fix this doesn't work due to a syntactic problem in Ocaml: type widget = ... and class window = ... and class button = ... is what you need: mutual recursion. But it isn't allowed to mutually recurse types and classes (not even class types). So a direct solution is unfortunately impossible. You must add a type parameter to each class like this: class ['w] button' = ... (childs: 'w list) class ['w] window' = .. .(childs: 'w list) ... and now you can define: type 'w widget' = [`Window of ['w] window' | ... Because these things all use a parameter, they can all be defined without any mutual recursion. So now you do this: (* fixpoint *) type widget = 'w widget' as 'w which effectively solves the type equation 'w = 'w widget (and names the result widget). Now define non-polymorphic classes: class button = [widget] button' class window = [widget] window' This is not so bad for a single parameter 'w. For multiple parameters the notation soon becomes too unwieldy to be practical. -- 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