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 DAA04438; Wed, 25 Jun 2003 03:06:09 +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 DAA04982 for ; Wed, 25 Jun 2003 03:06:07 +0200 (MET DST) Received: from saul.cis.upenn.edu (SAUL.CIS.UPENN.EDU [158.130.12.4]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h5P166j28852 for ; Wed, 25 Jun 2003 03:06:07 +0200 (MET DST) Received: from saul.cis.upenn.edu (localhost [127.0.0.1]) by saul.cis.upenn.edu (8.12.9/8.12.9) with ESMTP id h5P165mV012531 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO); Tue, 24 Jun 2003 21:06:06 -0400 (EDT) Received: (from bywang@localhost) by saul.cis.upenn.edu (8.12.9/8.12.9/Submit) id h5P165p8012530; Tue, 24 Jun 2003 21:06:05 -0400 (EDT) From: Bow-Yaw Wang Message-Id: <200306250106.h5P165p8012530@saul.cis.upenn.edu> Subject: Re: [Caml-list] Visitor pattern (was copy of parametrized object) To: garrigue@kurims.kyoto-u.ac.jp (Jacques Garrigue) Date: Tue, 24 Jun 2003 21:06:05 -0400 (EDT) Cc: caml-list@inria.fr In-Reply-To: <20030625092536F.garrigue@kurims.kyoto-u.ac.jp> from "Jacques Garrigue" at Jun 25, 2003 09:25:36 am X-Mailer: ELM [version 2.4 PL23-upenn3.3] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; bow-yaw:01 bywang:01 caml-list:01 extensible:01 val:01 jacques:01 ocaml:01 variants:01 int:01 garrigue:01 polymorphic:01 simpler:01 variant:02 string:03 coding:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Thanks a lot. Using polymorphic variants solves the problem beautifully. I'll reorganize my code. Thanks! Bow-Yaw > By the way, I'm not sure this coding of the visitor pattern is a good > programming approach in ocaml: most things can be done in a simpler > way using a function rather than a visitor, since anyway generally all > cases of a visitor have the same return type. > > # class a = > object > method invite : 'cases 'value. (([> `A] as 'cases) -> 'value) -> 'value = > fun f -> f `A > end;; > class a : object method invite : ([> `A ] -> 'a) -> 'a end > > The advantage is two-fold: you don't need to define a class for each > visitor (this is a pain), and now the variables 'cases and 'value are > really independent, so if you want to define the class a in a more > extensible way, you can write: > > # class ['cases] a = > object > method invite : 'value. (([> `A] as 'cases) -> 'value) -> 'value = > fun f -> f `A > end;; > class ['a] a : > object constraint 'a = [> `A ] method invite : ('a -> 'b) -> 'b end > # let x = new a;; > val x : _[> `A ] a = > # x#invite (fun `A -> 0);; > - : int = 0 > # x#invite (fun `A -> "a");; > - : string = "a" > > Another remark is that in most cases there is no need to wrap your > variant type in an object anyway, and then you don't need classes at > all :-) > > Jacques Garrigue ------------------- 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