Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] about Obj.magic
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] about Obj.magic
Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> writes:

> From: "Warp" <warplayer@free.fr>
>
> 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.

This is implemented in my hweak library, but using weak Hashtable (and
so an impl object will be freed if there is not any other things that
point to them).

(http://aspellfr.free.fr/)
-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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