From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id IAA23358 for caml-redistribution; Tue, 26 Oct 1999 08:52:19 +0200 (MET DST) 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 WAA00172 for ; Mon, 25 Oct 1999 22:38:22 +0200 (MET DST) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id WAA25555 for ; Mon, 25 Oct 1999 22:38:22 +0200 (MET DST) Received: from spi.lip6.fr (monbazillac.lip6.fr [132.227.82.65]) by isis.lip6.fr (8.8.8/jtpda-5.2.9.1+lip6) with ESMTP id WAA07139 for ; Mon, 25 Oct 1999 22:38:20 +0200 (MET DST) Received: from micmac.lip6.fr (micmac.ufr-info-p6.jussieu.fr [132.227.89.3]) by spi.lip6.fr (8.8.6/jtpda-5.2) with ESMTP id WAA03139 for ; Mon, 25 Oct 1999 22:38:18 +0200 (MET DST) Received: from (emmanuel@localhost) by micmac.lip6.fr (8.7.5/jtpda-5.2) id WAA01700 for caml-list@pauillac.inria.fr; Mon, 25 Oct 1999 22:33:49 +0200 (MET DST) Message-Id: <199910252033.WAA01700@micmac.lip6.fr> MIME-Version: 1.0 (NeXT Mail 3.3risc v118.3) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable In-Reply-To: <19991025114616.B226@jones.int-evry.fr> X-Nextstep-Mailer: Mail 3.3risc (Enhance 2.1) Received: by NeXT.Mailer (1.118.3) From: Emmanuel CHAILLOUX Date: Mon, 25 Oct 99 22:33:46 +0200 To: caml-list@pauillac.inria.fr Subject: Re: Very very short question on objects References: <19991025114616.B226@jones.int-evry.fr> Sender: weis Hi Christian, > Hi everybody, > > I'm getting started with OCaml objects and, trying to compose > functional updates, I ran into something like the following > toy example: > > Objective Caml version 2.02 > > # class x = > object > method private f = () > method g = {< >} # f > end > ;; > class x : object method f : unit method g : unit end > # > > Method [f] is not private... Shouldn't be this an error? Because you add a type constraint as follow : $ ocaml Objective Caml version 2.02 class t = object(self :'a) constraint 'a = < f : unit; ..> (* from {<>}#f *) method private f = () method g = {<>}#f end;; class t : object method f : unit method g : unit end If you have in the same class declaration, a method f with the = attribute private, and the same method f without it, the last definition wins. # class d = object method private f = print_string "private" = method f = print_string "public" end;; class d : object method f : unit end # let q = new d;; val q : d = # q#f;; public- : unit = () including the case where the last declaration is private. # class e = object method f = print_string "public" method = private f = print_string "private" end;; class e : object method f : unit end # let i = new e;; val i : e = # i#f;; private- : unit = () So the last declaration wins, includind a "private" declaration. The other examples show some type constraints : (* don't copy the instance to keep private method *) # class y = object(self) method private f = () method g = = self#f end;; class y : object method private f : unit method g : unit end (* It's same that your example *) # class z = object(self) method private f = () method g = = (Oo.copy self)#f end;; class z : object method f : unit method g : unit end (* Another case to add a type constraint (for the parameter o) *) # class u o = object(self:'a) method private f = () method g = (o : 'a) = o#f end;; class u : 'b -> object ('a) method f : unit method g : 'a -> unit = end (* to see an inherited private method *) # class v = object(self) inherit y as super method f = super#f = end;; class v : object method f : unit method g : unit end If you want to simulate the attribute "private", you can use a = functional instance variable as follows : # class p (h : 'b) = object (self : 'a) constraint 'b = 'a -> unit val f = h method g = f({<>}) end;; class p : ('a -> unit) -> object ('a) val f : 'a -> unit method g = : unit end # let h x = ignore(Oo.copy x); ();; val h : < .. > -> unit = # let ip = new p h;; val ip : p = # ip#g;; - : unit = () > Best regards, > > Christian Best regards, Emmanuel...=