From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: marant.logatique@fr.thalesgroup.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Using %identity for casting types
Date: Fri, 02 Aug 2002 23:29:31 +0900 [thread overview]
Message-ID: <20020802232931I.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20020802154858.A18074@fr.thalesgroup.com>
From: Jérôme Marant <marant.logatique@fr.thalesgroup.com>
>
> I'm doing some experiments in interfacing OCaml with C++ and
> it is sometimes necessary to cast one type to another.
> I've seen in lablgtk something like:
> external unsafe_cast : 'a obj -> 'b obj = "%identity"
>
> It is meant to be used by the end user?
Well, not really, but if you're interfacing with C++, it's not more
dangerous to do the cast in ocaml than in C++ (except if you use
rtti).
The above idiom is equivalent to
let unsafe_cast : 'a obj -> 'b obj = Obj.magic
It may be more efficient, as the compiler knows that unsafe_cast is
the identity, and can discard it. No code is generated.
I also tend to prefer it because it makes clear the need to properly
write types: such casts only make sense at a precise type, while
Obj.magic is 'a -> 'b, which allows too much.
And, as always, only use unsafe casts for non-ocaml data.
Ocaml datatypes are expressive enough that you don't need unsafe
casts for them.
Jacques Garrigue-------------------
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
next prev parent reply other threads:[~2002-08-02 14:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-02 13:48 Jérôme Marant
2002-08-02 14:29 ` Jacques Garrigue [this message]
2002-08-04 7:53 ` Jérôme Marant
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=20020802232931I.garrigue@kurims.kyoto-u.ac.jp \
--to=garrigue@kurims.kyoto-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=marant.logatique@fr.thalesgroup.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