Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>
To: fessant@pa.dec.com
Cc: caml-list@inria.fr
Subject: Re: Objects contrib: new URL ...
Date: Thu, 17 Jun 1999 11:26:33 +0900	[thread overview]
Message-ID: <19990617112633P.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: Your message of "Wed, 16 Jun 1999 08:56:41 -0700 (PDT)" <14183.51513.8527.191081@virtualc5.pa.dec.com>

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




  reply	other threads:[~1999-06-17 15:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-06-15 21:45 Objects contrib Fabrice Le Fessant
1999-06-16  8:13 ` Vyskocil Vladimir
1999-06-16 15:56 ` Objects contrib: new URL Fabrice Le Fessant
1999-06-17  2:26   ` Jacques GARRIGUE [this message]
1999-06-17 17:16     ` Fabrice Le Fessant
1999-06-17 18:44       ` Stefan Monnier
1999-06-18  1:19       ` Jacques GARRIGUE
1999-06-21 11:41 Don Syme

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=19990617112633P.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=fessant@pa.dec.com \
    /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