caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Michael Vanier <mvanier@cs.caltech.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Re: OCAML Downcasting?
Date: Tue, 21 Sep 2004 17:32:36 -0500 (CDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0409211722080.5809-100000@localhost.localdomain> (raw)
In-Reply-To: <20040921220621.92EA99BD95@orchestra.cs.caltech.edu>

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


  reply	other threads:[~2004-09-21 22:22 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <ci7tcf$qqf$1@wolfberry.srv.cs.cmu.edu>
     [not found] ` <ci9ggm$i6p$1@wolfberry.srv.cs.cmu.edu>
2004-09-21  8:03   ` Jacques GARRIGUE
2004-09-21  8:43     ` Damien Pous
2004-09-21  9:15       ` Jacques GARRIGUE
2004-09-21  9:29         ` skaller
2004-09-21  9:49           ` Jacques GARRIGUE
2004-09-21  9:34         ` Stefano Zacchiroli
2004-09-21  9:56           ` Jacques GARRIGUE
2004-09-21 19:27     ` Michael Vanier
2004-09-21 21:38       ` Brian Hurt
2004-09-21 22:06         ` Michael Vanier
2004-09-21 22:32           ` Brian Hurt [this message]
2004-09-22  1:04           ` skaller
2004-09-21 22:20         ` Marcin 'Qrczak' Kowalczyk
2004-09-22  2:26           ` skaller
2004-09-22  6:31             ` Marcin 'Qrczak' Kowalczyk
2004-09-22  9:03               ` sejourne_kevin
2004-09-22 10:29               ` Richard Jones
2004-09-22 18:39                 ` Brian Hurt
2004-09-22 10:50               ` skaller
2004-09-22 12:03               ` Alain Frisch
2004-09-22 12:50               ` Cláudio Valente
2004-09-22 13:15                 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 15:50                   ` skaller
2004-09-22 18:42               ` Brian Hurt
2004-09-22 18:44                 ` Marcin 'Qrczak' Kowalczyk
2004-09-22 19:18                   ` Brian Hurt
2004-09-22  0:50         ` skaller
2004-09-22  1:30       ` Jacques GARRIGUE
2004-09-22  2:59         ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0409211722080.5809-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=mvanier@cs.caltech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).