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-21 (14:00)
From: Boris Yakobowski <boris@y...>
Subject: Re: [Caml-list] obj.magic for polymorphic record fields
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.