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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Chemouil <David.Chemouil@e...>
Subject: Obj.magic


Hi again,



I'd like to get information about Obj.magic. I know it is some kind of
identity function `a -> `b, but I'd like to know how it can be used, why
it can be interesting and why, also, it is sometimes dangerous
(considering some older posts on this mailing-list). For example, I
found myself trying some different things (with polymorphic variants and
coercions, too):

# type arg = [`A | `B];;
type arg = [ `A | `B]
# let id : arg -> arg = function
  | `A -> `A
  | `B -> `B;;
val id : arg -> arg = <fun>
# let x = `A;;
val x : [> `A] = `A
# id x;;
- : arg = `A
# x;;
- : [> `A] = `A
# let y = ref `A;;
val y : _[> `A] ref = {contents=`A}
# id !y;;
- : arg = `A
# y;;
- : arg ref = {contents=`A}
# let z = Obj.magic `A;;
val z : '_a = <poly>
# id z;;
- : arg = `A
# z;;
- : arg = `A
# let t = (x :> arg);;
val t : arg = `A

-- 
David Chemouil [mailto:chemouil@enseeiht.fr] [mobile: 06 84 16 26 65]

Laboratoire d'informatique et de mathématiques appliquées (IRIT-INPT)

"Je vous ai fait trop faibles pour sortir du gouffre, parce que 
 je vous ai fait assez forts pour n'y point tomber" -- Rousseau