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: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] about Obj.magic
From: dengping zhu <zhudp@cs.bu.edu>

> When should I use 'Obj.magic'?

You shouldn't.

> It is really confusing. Now I am converting
> a bunch of code from sml to ocaml. During the process, I want to make
> sure that the type of every function keep consistent with the original
> one. But I still encountered a lot of problems about the types in ocaml. 
> For examples, all my base functions are fine in ocaml. But, sometimes when
> I tried to combine a few base functions together in the same way as my
> original code, I got a type error.

SML97 and ocaml typing are essentially the same, so you are probably
getting something else wrong.
If you can get the types of the functions you are trying to combine,
and the expression where you get a type error, and the result is not
too big, you may ask advice on this list.

> After endless trial, I have to use 'Obj.magic'. In some sense, it is
> good because I can compile my code now. On the other hand, things
> will be worse because it is very difficult to debug if I use a few
> 'Obj.magic' in my code. I couldn't find out the origins of all the
> runtime errors.

Obj.magic turns off _any_ checking. There is nothing like runtime type
checking.  The first error is a segmentation fault, or worse, an
impredictible program.

> So, we should be very careful to use 'Obj.magic'. Can somebody tell me
> the appropriate situation to use this function?

There are none. It is just there so you can do some stuff without
having to modify the C runtime, supposing that you understand what it
means in terms of machine execution. In particular, to do that you
_must_ know that the input and output types have equivalent
representations, which requires deep knowledge of the compiler.

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