From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id E820EBC84 for ; Thu, 31 Mar 2005 04:30:21 +0200 (CEST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j2V2UJWc010942 for ; Thu, 31 Mar 2005 04:30:21 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j2V2UEEm014840; Thu, 31 Mar 2005 11:30:14 +0900 (JST) Date: Thu, 31 Mar 2005 11:30:05 +0900 (JST) Message-Id: <20050331.113005.122575726.garrigue@math.nagoya-u.ac.jp> To: marc.herbert.1@ml.free.fr Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] private methods restricted to self? From: Jacques Garrigue In-Reply-To: References: <6b8a91420503221156e994d7c@mail.gmail.com> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Miltered: at concorde with ID 424B60BB.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 implicitly:01 ocaml:01 surprising:01 val:01 val:01 implicitly:01 supertype:01 jerome:01 vouillon's:01 ocaml:01 iirc:01 ocaml's:01 java's:01 wrote:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: From: Marc Herbert > On Tue, 22 Mar 2005, Remi Vanicat wrote: > = > > > I don't understand why my private subupdater is "made public impl= icitly" > = > > In ocaml, you can only call a private method on self. super#forward= > > is not self, so you cannot call the color_forward method on it. > = > Indeed! This other example, without any subclass, is even more > surprising: > = > class functional_point =3D > object(self) > val x =3D 0 > val y =3D 0 > method private bump_x =3D {< x =3D x + 1 >} > method private bump_y =3D {< y =3D y + 1 >} > = > method private bump_both =3D self#bump_x#bump_y > end > = > Warning: the following private methods were made public implicitly: > bump_y Exactly the same thing: self#bump_x returns a modified copy of self, so you cannot call private methods on it from outside. This would break object encapsulation. This is exactly for this reason that the functional object update allows to modify several fields simultaneously. One must distinguish between 1) self (the same object) 2) object of the same class (obtained via update for instance) 3) object sharing a common supertype (including object of self type) Only (1) allows private method calls. (2) technically could, but the typing cannot properly detect this situation, and this would break object encapsulation anyway. (3) may be from a completely unrelated class, so there is no way to know its private methods. > Thanks a lot for the reasoning. But I still don't get the rationale. > Could I find the answer in Jer=F4me Vouillon's "an object calculus wi= th > views"? Elsewhere? Thanks in advance. Not there, since views are not implemented in ocaml. And the original paper on object ML does not deal with private methods, IIRC. So your only source is the reference manual I suppose. > About the reference documentation > --------------------------------- > = > However ignorant are my questions above, I nevertheless think the > documentation is not clear about this whole issue. > = > To find this self#private restriction you have to dive into section > '6.9.2 Class expressions'. Unfortunately you may never go so far sinc= e > the more user-friendly '3.6 Private methods' section gives the less > restrive, more usual definition: > = > "Private methods are methods that do not appear in object > interfaces. They can only be invoked from other methods of the same= > object." > = > which made me think I knew enough about 'private' and prevented me to= > look further. This sentence describes exactly the behaviour of private methods, and you don't need to go further. You should look carefully at: from other methods of the _same_ object. We are talking here of objects, not of classes. As soon as you are trying to access another object (even if it is of the same class) you cannot call private methods. > Section '6.9.1 class types' also states: > = > "The flag private indicates whether the method can be accessed from= > outside the class." > = > Coming from another OO-language, it's hard to understand "outside" as= > "restricted to self". This statement is indeed a bit confusing. This should be "from outside = the object". To make it complete, I would write "The flag private indicates whether the method can be accessed from outside the object (i.e. whether it appears or not in the object type.)" > So as a conclusion I find the manual at least confusing on this, if > not erroneous. Is it? It is mostly correct. The main problem is that ocaml's notion of privacy is very different from Java's, so that people have preconceptions. From the point of view of encapsulation, the ocaml approach is more natural, don't you think so? > By the way, maybe some extra comment in the Warning message above > would not hurt either. There are various ways to make a private method public, and they are not all easy to detect individually, so there is little more that can be said in general. I'll look into it. Jacques Garrigue