From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA19757; Sun, 13 Jun 2004 20:00:02 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA19933 for ; Sun, 13 Jun 2004 20:00:00 +0200 (MET DST) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i5DHxvSH020983 for ; Sun, 13 Jun 2004 19:59:59 +0200 Received: from [192.168.1.200] (ppp219-112.lns2.syd3.internode.on.net [203.122.219.112]) by smtp1.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i5DHxp4Y023592; Mon, 14 Jun 2004 03:29:52 +0930 (CST) Subject: Re: [Caml-list] troubles with polymorphic variant in class From: skaller Reply-To: skaller@users.sourceforge.net To: IdeFX@Iname.com Cc: caml-list In-Reply-To: <1087142756.10383.50.camel@Bess> References: <1087142756.10383.50.camel@Bess> Content-Type: text/plain; charset=UTF-8 Message-Id: <1087149590.16811.1342.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 14 Jun 2004 03:59:51 +1000 Content-Transfer-Encoding: 8bit X-Miltered: at concorde with ID 40CC961D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 sourceforge:01 2004:99 widget:01 widget:01 widgets:01 val:01 childs:01 childs:01 recursion:01 recursion:01 fixpoint:01 unwieldy:01 9660:01 glebe:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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