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 LAA00291; Tue, 21 Sep 2004 11:15:47 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA00458 for ; Tue, 21 Sep 2004 11:15:45 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i8L9FhJg019673 for ; Tue, 21 Sep 2004 11:15:44 +0200 Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2-20030924/3.7W) with ESMTP id SAA26749; Tue, 21 Sep 2004 18:15:31 +0900 (JST) Date: Tue, 21 Sep 2004 18:15:31 +0900 (JST) Message-Id: <20040921.181531.71542839.garrigue@kurims.kyoto-u.ac.jp> To: Damien.Pous@ens-lyon.fr Cc: caml-list@inria.fr Subject: Re: [Caml-list] Re: OCAML Downcasting? From: Jacques GARRIGUE In-Reply-To: <20040921104306.0e472a0e@localhost.localdomain> References: <20040921.170339.118021840.garrigue@kurims.kyoto-u.ac.jp> <20040921104306.0e472a0e@localhost.localdomain> X-Mailer: Mew version 4.0.64 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 414FF13F.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 downcasting:01 jacques:01 damien:01 damien:01 ens-lyon:01 jacques:01 widget:01 widget:01 prerr:01 endline:01 ocamlc:01 prerr:01 endline:01 val:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Damien Pous > Jacques GARRIGUE wrote: > > Just for fun, here is an example of the second approach: > > > > class virtual ['a] widget = object > > method virtual raw : 'a > > end > > class ['a] button = object (self) > > inherit ['a] widget > > method raw = `Button self > > method press = prerr_endline "pressed" > > end > > How to write a signature for this code ? > > even "ocamlc -i" fails ! You're right. I shall never post before verifying my code seriously. The real code is: class virtual ['a] widget = object method virtual raw : 'a end class ['a] button = object (self) inherit ['a] widget method raw = `Button (self :> 'a button) method press = prerr_endline "pressed" end class ['a] label = object (self) inherit ['a] widget method raw = `Label (self :> 'a label) val mutable text = "" method set_text s = text <- s end let l = [(new button :> _ widget); (new label :> _ widget)] let f = function `Button b -> b#press | `Label l -> l#set_text "Hello" let test () = List.iter (fun o -> f o#raw) l Without the self-cast, you cannot extend any class. 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