caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Valentin Bonnard <bonnard@clipper.ens.fr>, caml-list@inria.fr
Subject: Re: Problème d'oubli dans un module [A problem with modules]
Date: Sun, 7 Nov 1999 15:21:16 +0100	[thread overview]
Message-ID: <19991107152116.28401@pauillac.inria.fr> (raw)
In-Reply-To: <19991030044139.30767@soling.ens.fr>; from Valentin Bonnard on Sat, Oct 30, 1999 at 04:41:39AM +0200

> [ I can still use ``get'': ]
> 
> # let z = Obj.magic y;;
> val z : '_a = <poly>
> # print_int (z#get);;
> 3- : unit = ()
> 
> [ And it works, I understand that it is guarantied to work. ]

No, it's not.  To begin with, any piece of code that contains
"Obj.magic" is not guaranteed to work.  (You'd be amazed at how many
type-based compiler optimizations can be broken by innocuous-looking
uses of Obj.magic.  I spent one full day once tracking a GC "bug" in
ocamlopt that had been reported to us; the "bug" turned out to be one
such use of Obj.magic in the user's code.  Since then, I've been
grepping for "Obj.magic" in every bug report that we get, and throwing
away immediately those where it occurs.  You've been warned!)

Second, you're making assumptions on how subtyping coercions are
compiled.  (See below.)

> [ Now, I will try to do the same thing with modules. ]
> [ Now, how can I use ``get'' ? ]
> # (Obj.magic Y: sig val get : int end).get;;

You can't.  First of all, there is no module-level equivalent of
Obj.magic.  (Thanks God.)  Second, the run-time representation of Y
really contains no "get" member, it's just the empty tuple.

Subtyping between modules is coercitive, meaning that at the point
where a module is viewed with a supertype, the run-time representation
of the module changes to reflect the change in type.  This allows to
compile accesses to structure fields by offsets (computed from the
signature of the structure) rather than by name, which is good for
efficiency.

In contrast, subtyping between objets doesn't change the object
representation in the OCaml compiler.  There are several reasons for
this, one being that it makes it much easier to preserve object
identity (for objects having mutable instance variables).  But one
could come up with compilation schemes where subsumption on objects
changes the internal representation.

> [ I'd like to access the forgotten get member.
> In case you are wondering, yes, this need arises 
> from a real problem. ]

What about looking for a type-safe solution to that real problem?

- Xavier Leroy



      reply	other threads:[~1999-11-07 21:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-10-30  2:41 Valentin Bonnard
1999-11-07 14:21 ` Xavier Leroy [this message]

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=19991107152116.28401@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=bonnard@clipper.ens.fr \
    --cc=caml-list@inria.fr \
    /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).