Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Type hidding
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Type hidding
From: "Warp" <warplayer@free.fr>

> Ok I've tried this doing :
> 
>  (set_window_data wnd#hwnd 10);
>  let v = (get_window_data wnd#hwnd) in
>      (print_int (Obj.magic v: int));
> 
> But it print a wrong number ( not 10 )
> I've tried to trace calls into the C DLL where primitives *_window_data are,
> I can see the set_window_data call but the get_window_data call is NOT done
> by the interpreter

Aaargh! No, never use Obj.magic.
And please, never suggest to use it to resolve a type error.
Its only use is in relation to external C code, when you exactly know
what is happening!

Here the problem is that the type of v ends up being "wnd:hwnd -> 'a"
rather than "'a" as you expected. Since you omitted the label on the
argument, and the result type could be unified with anything, the
compiler believed that you were attempting to pass another argument...
By using magic, you are printing the address of a closure as an
integer.

Basic rules:
* a function should never return 'a if it does not also appear in a
  contravariant position (that is, as argument)
* it is better not to put labels on functions of only one argument,
  particularly when their return type is polymorphic (to avoid the
  above confusion)

The right response to your problem was already in other messages.

Cheers,

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr