English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re[2]: [Caml-list] obj.magic for polymorphic record fields
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-12-22 (13:36)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] obj.magic for polymorphic record fields
Boris Yakobowski <boris@yakobowski.org> writes:

> On Mon, Dec 21, 2009 at 2:44 PM, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>>> However it issues a warning so i acknowledge it's less elegant.
>> Which I don't quite understand.
> The warning is based on the results of the type inference algorithm.
> You're not supposed to find values of type 'a. 'a "in the wild", yet
> (Obj.magic a) has this type. Thus the Ocaml compiler deduces that
> you're doing something wrong: your function never returns, it does not
> use its arguments, etc. Here, using Obj.magic breaks the crystal ball
> of the compiler, and the warning is incorrect.
> There is an easy way to silence the warning though:
> let baz = {bar = fun x -> (Obj.magic a : _ -> _) x}
> (but not let baz = {bar = fun x -> (Obj.magic a : 'a -> 'a) x} for
> ugly reasons...)
> You are however quite correct. Using Obj.magic in this particular case
> seems quite wrong.
> -- 
> Boris

But the type inference should deduce that in

(Obj.magic fn) x

the 'a is actually 'b -> 'c as I am applying an argument to it.
Seems to me there is something special for Obj.magic so it doesn't
automatically cast values to functions.