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 CAA02290; Wed, 22 Sep 2004 02:50:51 +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 CAA03237 for ; Wed, 22 Sep 2004 02:50:50 +0200 (MET DST) Received: from smtp3.adl2.internode.on.net (smtp3.adl2.internode.on.net [203.16.214.203]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i8M0olnb014082 for ; Wed, 22 Sep 2004 02:50:49 +0200 Received: from [192.168.1.200] (ppp202-133.lns1.syd3.internode.on.net [203.122.202.133]) by smtp3.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i8M0oYOU008641 for ; Wed, 22 Sep 2004 10:20:45 +0930 (CST) Subject: Re: [Caml-list] Re: OCAML Downcasting? From: skaller Reply-To: skaller@users.sourceforge.net To: caml-list In-Reply-To: References: Content-Type: text/plain Message-Id: <1095814233.2580.921.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 22 Sep 2004 10:50:34 +1000 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4150CC67.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 sourceforge:01 2004:99 38,:01 downcasting:01 nominal:99 vastly:01 nominal:99 subtyping:01 supertype:01 supertype:01 unions:01 natively:01 downcasts:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, 2004-09-22 at 07:38, Brian Hurt wrote: > Downcasting is a sign you're doing something wrong. If you knew the > object was a bar_t, why didn't it have the type bar_t from the get-go? I agree. However I'll add two explanations here. (1) C++ and Java classes use nominal typing which means they have a particular name. Two similar classes with a different name are different. This is important because the associated type is determined exclusively by the name. In Ocaml, the class name is irrelevant because they use structural typing. This means that what counts is the set of names of the methods and their types. So in Ocaml two similar classes with the different names have the *same* associated type. Of course you can name such types, but the names are just aliases. Structural typing is prone to errors where two types intended to be distinct become interchangeable "by accident" because they have the same method names and types "by accident" but in practice this hardly ever happens. OTOH structural typing is vastly superior to nominal typing when it comes to subtyping. Algorithms can be written requiring a particular type, and can be applied to any class that has that type as a supertype, just as in Java or C++. The difference is you don't have to invade the class definition and add the name of the supertype to the list of abstract bases/interfaces. Therefore, the most fundamental principle of Object Orientation -- Meyers Open/Closed principle -- is obeyed by Ocaml classes and NOT by C++ or Java. It is this principle which helps provide encapsulation. You could say Ocaml is more Object Oriented than either C++ or Java because it obeys the major principles better. (2) In Ocaml we have two kinds of unions: ordinary variants and polymorphic variants, aka sum types. In C++ and Java, we don't have these types natively. [C++ has a severely broken union] So what programmers do to emulate them is use inheritence. This is the primary reason both C++ and java require downcasts. So in summary: OO programmers are generally ignorant of the very fundamentals of programming theory. They're used to dealing with all sorts of weird problems representing simple data which arises directly out from broken type systems C++ and Java provide: a naturally algebraic representation can't be found, because they only provide products and not sums. So the programmer turns to abstraction, but then has to 'unabstract' the abstraction using downcasts. The worst thing about this is that inheritance is used to provide two fundamentally distinct type relations: abstraction/subtyping and unification. In Ocaml, there are two distinct mechanisms. No one would claim Ocaml is perfect, but it does get the basics right. Note I didn't mention the fact you also have first class function types: this has nothing to do with functional programming per se. Its just that languages like Ocaml were designed after doing basic mathematics, Java and C++ were not. -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- 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