Re: Objects contrib: new URL ...

From: Jacques GARRIGUE (garrigue@kurims.kyoto-u.ac.jp)
Date: Thu Jun 17 1999 - 04:26:33 MET DST


To: fessant@pa.dec.com
Subject: Re: Objects contrib: new URL ...
In-Reply-To: Your message of "Wed, 16 Jun 1999 08:56:41 -0700 (PDT)"
        <14183.51513.8527.191081@virtualc5.pa.dec.com>
Message-Id: <19990617112633P.garrigue@kurims.kyoto-u.ac.jp>
Date: Thu, 17 Jun 1999 11:26:33 +0900
From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>

> I have made a small patch to ocaml-2.02 to allow safe casts of objects.
> The patch adds two new keywords "implements" and "cast".

This looks both interesting and dangerous.

Interesting:
 Lots of people want such a feature. This is standard in
LISP or JAVA, so why not in Caml?
 A slight improvement: I would rather have cast raise Match_failure
than a standard Failure. In particular with a match failure you have
position information for where it failed.

Dangerous:
 I believe this really goes against what ML is trying to be.
It allows people to write their programs like they would do in JAVA,
that is in a partially unsafe way. "I believe this object will have
type t, so let's cast it to t." What makes you believe so? I don't see
any guarantee for that.
 Another improvement for that would be to use a match construct rather
than only cast, and to have a warning if the match does not contain a
wild card at the end.

 Another problem is that it creates a distinction between classes with
parameters and classes without, which is not very natural.

 It would have been easy to introduce such a feature in the object
system from the beginning: caml objects bear a pointer to their class,
you just have to put the class name in the class. My guess is that it
was intentionnally left out.

Remark:
 You can already implement something similar inside the language:

 For each class c create an hash-table referencing the object
 (In fact this should not be a standard hash-table, but a set of weak
  pointers, otherwise your objects will never be GCed as long as you
  don't remove them from the table)

        type empty_obj = < >
        let memo_c : (empty_obj, c) Hashtbl.t = Hashtbl.create 7

        let new_c args =
          let obj = new c args in
          Hashtbl.add memo_c (obj :> empty_obj) obj;
          obj

        let cast_c obj = Hashtbl.find memo_c (obj :> empty_obj)

 Do not forget to catch Not_found when using cast_c !
 This works for parameterized classes also: you just have to create a
different memo for each parameters.
 You don't have the flexibility of the implements feature, but you
could also implement it by defining cast_c to look into the memos of
all subclasses in the implement hierarchy.

Regards,

        Jacques
---------------------------------------------------------------------------
Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp
                <A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:23 MET