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 OAA07138; Wed, 18 Aug 2004 14:04:47 +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 OAA07059 for ; Wed, 18 Aug 2004 14:04:46 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from mta07-svc.ntlworld.com (mta07-svc.ntlworld.com [62.253.162.47]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i7IC4jRM015661 for ; Wed, 18 Aug 2004 14:04:45 +0200 Received: from [81.107.63.30] by mta07-svc.ntlworld.com (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP id <20040818120507.EIAZ565.mta07-svc.ntlworld.com@[81.107.63.30]>; Wed, 18 Aug 2004 13:05:07 +0100 Message-ID: <412345DC.5050206@ntlworld.com> Date: Wed, 18 Aug 2004 13:04:44 +0100 From: "chris.danx" User-Agent: Mozilla Thunderbird 0.7.3 (X11/20040803) X-Accept-Language: en-us, en MIME-Version: 1.0 To: John Prevost CC: Caml Mailing List Subject: Re: [Caml-list] Restricting Method Overriding/Redefinition in Subclass References: <411D56EC.2070301@ntlworld.com> <1092469137.29139.564.camel@pelican.wigram> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 412345DD.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; ntlworld:99 caml-list:01 overriding:01 prevost:01 subtyping:01 subtyping:01 subclassing:01 subclassing:01 enforced:01 inference:01 someway:01 runtime:01 ocamls:01 subtypes:01 reuse:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk John Prevost wrote: [snip] > let use_get_x o = o#get_x > > Here, the function "use_get_x" can be used on instances of any class > that contains a method get_x : unit -> int. This is called > "structural subtyping". The subtype relationship is based purely on > what methods exist, not on what classes have been declared to be > subclasses of others. I didn't realise OCaml supported this. I wanted to write code like that but didn't realise it was possible in OCaml. This opens the door to a new way of writing programs. Thanks! > At this point, you may be wondering "Why is O'Caml like this? Is it a > good thing to separate subtyping and subclassing this way?" So I'll > give a couple of examples of how this type discipline is less of a > pain than the one used by Java. It's not so strange to me. Separating subtyping from subclassing is something I'd read about (in the context of polymorphism and subclassing) and thought was very useful. It gives you the ability to write code that works over different classes of object, without specifying the precise type of the object (it's type is implied by the methods it has). This is why I didn't realise OCaml provides the ability to do this. I'd assumed that the typing rules of OCaml enforced the fact that the methods and classes had to exist before the inference of the type was done and that it'd be one of those classes. Thinking about it that doesn't make much sense. Now it's clear how the new immediate objects work. It was a bit puzzling before, as there seemed no way to use them. Immediate objects seem to make it trivial to write something like a proxy providing you know the methods before hand. A more interesting case would be if you didn't know the methods before hand but you could access them in someway and build objects at runtime and you could somehow guarantee the result was compatible. Not sure how that'd fit with OCamls typing scheme, but it's interesting to think about. > 1) Implementing multiple interfaces. ... > 2) Subclasses that are not subtypes ... The first one is easy to understand, but the second example took a while to get my head around. I think I understand the implications of it now, although it is still a bit unclear to me how the reuse works in this example. I sort of understand it, but bits of it are unclear. Need to think about it for a while. > I hope my long-winded explanation was useful. Very! OCaml is a big language - in the sense there's a lot of power there - and some of it is still hidden to me so I appreciated your explanation. Thanks, Chris ------------------- 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