caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: warplayer@free.fr
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] about Obj.magic
Date: Fri, 31 May 2002 22:58:40 +0900	[thread overview]
Message-ID: <20020531225840C.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <003601c20884$0d78efd0$7900a8c0@warp>

From: "Warp" <warplayer@free.fr>

> > So, we should be very careful to use 'Obj.magic'. Can somebody tell me
> > the appropriate situation to use this function?
> 
> One case can be 'almost-legal' : downcasting an object to its implementation
> class if you're sure that it has been previously upcasted to the interface
> type class.
> 
> (* MLI file *)
> 
> class type intf =
>   sig
>     method a : unit
>   end
> 
> val new_intf : intf -> intf
> 
> (* ML file *)
> 
> class impl parent =
>   object
>     method a = ....
>     method b = ....
>   end
> 
> let new_intf i = (new impl (Obj.magic i : impl) :> intf) (* upcast impl to
> intf in order to hide method 'b' *)

And here is how to break your code:

class c = object method a : unit end
let i = new_intf (new c)

No, this is not legal in any way.
You shouldn't use Obj.magic, except if you have a formal proof that it
is correct! This supposes a deep knowledge of the type system.

By the way, here is a simple way to do downcasts, when really needed.

# let tbl : (< >, impl) Hashtbl.t = Hashtbl.create 17
  let register obj = Hashtbl.add tbl (obj :> < >) obj
  let recover obj = Hashtbl.find tbl (obj :> < >) ;;
val tbl : (<  >, impl) Hashtbl.t = <abstr>
val register : impl -> unit = <fun>
val recover : < .. > -> impl = <fun>

If you register all objects of class impl, then you can use recover to
get back their original type. If the class is not impl, you will get
an exception rather than a segmentation fault. No need to do anything
unsafe.

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


  reply	other threads:[~2002-05-31 13:59 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-30 21:09 dengping zhu
2002-05-30 22:38 ` Jacques Garrigue
2002-05-31  4:00 ` Daniel de Rauglaudre
2002-05-31  5:37   ` Oleg
2002-05-31  6:17     ` Daniel de Rauglaudre
     [not found]       ` <p05100300b91cea02dbf5@[192.168.1.22]>
2002-05-31  9:50         ` [Caml-list] SML->OCaml (was: about Obj.magic) Daniel de Rauglaudre
2002-05-31 15:39   ` [Caml-list] about Obj.magic dengping zhu
2002-05-31 16:28     ` John D. Barnett
2002-06-01  8:37     ` Jacques Garrigue
2002-06-01 16:00       ` dengping zhu
2002-06-01 17:20         ` Pierre Weis
2002-05-31  9:17 ` Warp
2002-05-31 13:58   ` Jacques Garrigue [this message]
2002-05-31 14:06     ` Warp
2002-05-31 18:21       ` John Max Skaller
2002-06-01 17:42         ` Pierre Weis
2002-06-02 15:15           ` John Max Skaller
     [not found]           ` <D37FA3E3-763B-11D6-BE8F-0003938819CE@inria.fr>
2002-06-04 10:22             ` Pierre Weis
2002-05-31 14:10     ` Remi VANICAT
2002-05-31 16:19     ` Christophe TROESTLER
2002-05-31 18:06       ` Olivier Andrieu
2002-05-31 22:03         ` Christophe TROESTLER

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=20020531225840C.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=warplayer@free.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).