caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
To: Brian Hurt <bhurt@spnz.org>
Cc: Michael Vanier <mvanier@cs.caltech.edu>,
	garrigue@kurims.kyoto-u.ac.jp, <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: OCAML Downcasting?
Date: Wed, 22 Sep 2004 00:20:51 +0200	[thread overview]
Message-ID: <87isa76z7g.fsf@qrnik.zagroda> (raw)
In-Reply-To: <Pine.LNX.4.44.0409211619370.5809-100000@localhost.localdomain> (Brian Hurt's message of "Tue, 21 Sep 2004 16:38:25 -0500 (CDT)")

Brian Hurt <bhurt@spnz.org> writes:

> The general solution is to rethink the problem to avoid the downcast.

It's not always possible or desirable.

Example 1: exceptions. Fortunately OCaml provides an extensible exn
type. It's probably impossible to emulate using other features:
classes, polymorphic variants, modules etc., without Obj.magic.
OTOH dynamically typed or OO languages typically don't need extra
language features just to *represent* exceptions of extensible types.

Example 2: embedding a dynamically typed sublanguage in OCaml, with
extensible set of types of wrapped objects. For this I had to use
Obj.magic with manual tagging of dynamic types.

Attempts to avoid downcasts are often unmodular, they require to
specify all possible variants in one place.

>> Java now has generics (type parameters), and I assume that it has to get
>> around this situation somehow.
>
> If I understand it correctly, they basically lifted C++'s templates more 
> or less verbatim.

AFAIK the implementation technique is entirely different, similar to
parametric polymorphism in OCaml. It influences the semantics. You
can't select overloaded methods basing on a type substituted for a
type variable for example (except by using reflection at runtime);
you can do that in C++. There are no specializations of generics.
There are no types depending on types (an equivalent of putting
different typedefs in different specializations of a template).

Java generics compared to C++ templates are like OCaml parametric
polymorphism compared to OCaml modules.

> Downcasting is a sign you're doing something wrong.

I disagree. It may be, or not. Just that OCaml doesn't support
something doesn't imply that it must be evil.

> If you knew the object was a bar_t, why didn't it have the type
> bar_t from the get-go?

For the same reason as why can't exn be a regular algebraic type.

> If you don't know it's a bar_t, how do you know it's safe to cast it
> to a bar_t? Without RTTI, that is.

Of course something is necessary to determine at runtime whether it's
safe. I don't advocate unsafe constructs.

> By the way, RTTI costs memory (at least one extra word per object,
> plus the overhead of the Class classes for all types).

You could move the object header there. It would only make no room for
two bits for GC that OCaml uses, so it would be hard to adapt to OCaml
as it is, but it's not a significant overhead in general.

A dynamically typed language can have the overhead of one word per
object, for most objects. Only the size of arrays and strings is
stored after the header, but it would be a good idea anyway compared
to the current design - it would remove the artificial limit of object
sizes.

> Plus- does RTTI apply to non-objects? Ocaml has a lot of types that
> are not classes.

A possible answer: conceptually yes, physically it can be avoided for
unboxed ints or other unboxed variables whose type is known statically;
constants like the empty list can be boxed and allocated statically,
so they have almost the same overhead as currently (only the bit
pattern is not known until link time).

I don't claim that it would be easy to adapt to OCaml as it is.
I claim that it's not a bad idea in general.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/

-------------------
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


  parent reply	other threads:[~2004-09-21 22:21 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
2004-09-22  1:04           ` skaller
2004-09-21 22:20         ` Marcin 'Qrczak' Kowalczyk [this message]
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=87isa76z7g.fsf@qrnik.zagroda \
    --to=qrczak@knm.org.pl \
    --cc=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=garrigue@kurims.kyoto-u.ac.jp \
    --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).