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 AAA29183; Wed, 22 Sep 2004 00:22:41 +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 AAA29878 for ; Wed, 22 Sep 2004 00:22:39 +0200 (MET DST) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i8LMMRmV020354 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Wed, 22 Sep 2004 00:22:37 +0200 Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.13.1/8.12.11) with ESMTP id i8LMMJWe019394 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 21 Sep 2004 17:22:21 -0500 (CDT) Date: Tue, 21 Sep 2004 17:32:36 -0500 (CDT) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Michael Vanier cc: caml-list@inria.fr Subject: Re: [Caml-list] Re: OCAML Downcasting? In-Reply-To: <20040921220621.92EA99BD95@orchestra.cs.caltech.edu> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde with ID 4150A9A3.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 vanier:01 generic:01 casts:01 unboxing:01 multimethods:01 downcast:01 multimethods:01 downcasting:01 compiler:01 tagged:01 ocaml:01 ocaml:01 jvm:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, 21 Sep 2004, Michael Vanier wrote: > Um, no, they didn't. In fact, it's a completely different mechanism. The > compiler erases the generic information so that the JVM sees only old-style > java classes without parameterization and adds casts where needed. OK, > this wasn't the greatest example in the world, because it relies massively > on RTTI. OK. I'm not up on what precisely Java is doing here. I note that they're also adding autoboxing/unboxing. > I don't agree. For instance, try implementing the equivalent of > multimethods without some kind of downcast. Of course, if a language > supported multimethods from the get-go it would be even nicer, but very few > languages do. Now, if you're going to argue that wanting multimethods at > all is a sign that you haven't thought through a problem carefully enough, > we'll just have to agree to disagree. I'm always suspicious of arguments > that start off with "you really don't want to do that", because I can't say > with any certainty what I will want to do or need to do 100% of the time. Multimethods should use variant (tagged) types, not objects and downcasting. For example, consider the case where I want to deal with a number, that could be an integer, a floating point number, or a complex (x + yi format) number. I'd implement it like: type number_t = | Int of int | Float of float | Complex of float * float ;; let add a b = match a, b with | Int(x), Int(y) -> Int(x + y) | Int(x), Float(y) | Float(y), Int(x) -> Float((float_of_int x) +. y) | Float(x), Float(y) -> Float(x +. y) | Int(x), Complex(yr, yi) | Complex(yr, yi), Float(x) -> Complex(((float_of_int x) +. yr), yi) | Float(x), Complex(yr, yi) | Complex(yr, yi), Float(x) -> Complex(x +. yr, yi) | Complex(xr, xi), Complex(yr, yi) -> Complex(xr +. yr, xi +. yi) ;; This is what I meant by not everything in Ocaml needs to be objects. Note that there is an advantage to how Ocaml does it- if you add a new tag to number_t, Ocaml will warn you in all the places you need to update to handle the new tag. -- "Usenet is like a herd of performing elephants with diarrhea -- massive, difficult to redirect, awe-inspiring, entertaining, and a source of mind-boggling amounts of excrement when you least expect it." - Gene Spafford Brian ------------------- 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