From: "David Allsopp" <dra-news@metastack.com>
To: <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] safe casting
Date: Wed, 22 Aug 2007 23:20:01 +0100 [thread overview]
Message-ID: <00b501c7e50a$958abe40$6a7ba8c0@treble> (raw)
In-Reply-To: <72195753-8535-4D6F-BD60-79ED3C21B5D9@research.att.com>
> Given two record types that are identical but in name, is it safe (if
> perhaps a poor idea) to cast between functions that are parametric in
> these record types:
Yes, it's safe because the representations of the data are the same -
Obj.magic essentially removes all the protection of the type system by
giving you an 'a -> 'b function. And, yes, it's a bad idea unless you have a
*really* good reason to do it and a lot of time to comment your code ;o)
...
> Put another way: is the implementation of two records w/ identical labels
> the same, or is there a runtime significance to their static difference?
The labels of a record are only of concern to the type checker - they are
discarded at runtime. Two records will have the same runtime representation
iff the types of each member (reading left -> right) are the same.
e.g. type t = {a : int; b : int}
and type u = {c : int; d : int}
are the same at runtime. I *think* you can also get away with:
type t = {a : int; b : int}
type u = {c : int; d : int; e : int}
(Obj.magic {c = 3; d = 4; e = 5} : t)
i.e. a "downcast" of a variable of type u to one of type t: but I'm not sure
that would "work" in all contexts (especially where copying may be
concerned).
However, it's all terribly bad programming practice in O'Caml because you
end up with code that is totally resistant to change and that relies on the
Obj module (which is not officially supported). Chapter 18 (Interfacing with
C) of the manual helps with understanding the underlying representations of
values at runtime.
HTH,
David
prev parent reply other threads:[~2007-08-22 22:20 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-22 18:33 Yitzhak Mandelbaum
2007-08-22 22:20 ` David Allsopp [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='00b501c7e50a$958abe40$6a7ba8c0@treble' \
--to=dra-news@metastack.com \
--cc=caml-list@yquem.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