caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Using %identity for casting types
@ 2002-08-02 13:48 Jérôme Marant
  2002-08-02 14:29 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Jérôme Marant @ 2002-08-02 13:48 UTC (permalink / raw)
  To: caml-list

Hi,

  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?

  Thanks.

-- 
Jérôme Marant
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Using %identity for casting types
  2002-08-02 13:48 [Caml-list] Using %identity for casting types Jérôme Marant
@ 2002-08-02 14:29 ` Jacques Garrigue
  2002-08-04  7:53   ` Jérôme Marant
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2002-08-02 14:29 UTC (permalink / raw)
  To: marant.logatique; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Using %identity for casting types
  2002-08-02 14:29 ` Jacques Garrigue
@ 2002-08-04  7:53   ` Jérôme Marant
  0 siblings, 0 replies; 3+ messages in thread
From: Jérôme Marant @ 2002-08-04  7:53 UTC (permalink / raw)
  To: caml-list

Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> writes:

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

Thanks a lot.

Cheers,

-- 
Jérôme Marant

http://marant.org
              
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-08-04 22:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-02 13:48 [Caml-list] Using %identity for casting types Jérôme Marant
2002-08-02 14:29 ` Jacques Garrigue
2002-08-04  7:53   ` Jérôme Marant

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