Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Safe Obj.magic container ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-02-29 (01:52)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Safe Obj.magic container ?
From: David Teller <>

> Interesting. Can I assume that, if my type is boxed (in this case, a
> polymorphic variant), I can successfully convert it to Obj.t and back ?

Yes : polymorphic variants are either ints or normal blocks, so this
should work. This is also true for normal variants. Actually I don't
know of any non-uniform representation problem outside of floats.

Note however that this problem of non-uniform representation is no the
only danger when using Obj.magic imprudently. I recall another problem
with functional values. I'm afraid only Xavier could explain that one.

If you have only (polymorphic) variants, and if you keep the types
monomorphic (i.e. always add complete type annotations to Obj.magic or
Obj.obj), things should work properly in the current implementation.

Of course you should limit that kind of uses to things like
persistant storage or C interfaces, where you have to go through an
untyped world anyway, and avoid it at all costs in plain ml programs.

Jacques Garrigue