English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    
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: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] about Obj.magic
> An 'almost legal' use of Obj.magic is to populate an array with dummy 
> values.
> For example:
> 
>     class Deep  ..
>     let a = Array.make 100  (Obj.magic 0 :> Deep) in
>     let len_a = ref 0 in
> 
> This is roughly as safe as an unsafe array access,
> only now the array length is a variable.
> 
> -- 
> John Max Skaller, mailto:skaller@ozemail.com.au
> snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
> voice:61-2-9660-0850

I'm afraid that this use is not ``almost legal'' and, on the contrary,
it is strongly discouraged by good programming practice!

However, your example is interesting as a good counterexample: this is
typical of a bad usage of Obj.magic! The program you gave is indeed
wrong, unless you have a proof that all the elements of array ``a''
will be assigned before accessed, and may be also you have an
additional proof that the memory management system of Caml can deal
with the integer "0" as a valid object of the class Deep. Hence, this
occurrence of Obj.magic is unsafe, error prone, and should not be
encouraged.

The rule of thumb for usages of Obj.magic is as Jacques already stated
it: you need a theorem to write Obj.magic. That's the way the Caml
system uses it: we always have a meta theorem that proves that this
very occurence of Obj.magic is safe. Otherwise, the resulting program
has always the same property : one day or another, it gives you Bus
error.

Thank you for your example that will help Caml users.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


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