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
[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: 2002-01-16 (23:32)
From: Ceri Storey <cez@p...>
Subject: Re: [Caml-list] Type hidding
On Wed, Jan 16, 2002 at 09:09:48PM +0100, Warp wrote:
> external set_window_data : wnd:hwnd -> 'a -> unit = "set_window_data"
> external get_window_data : wnd:hwnd -> 'a = "get_window_data"
> I know that's very type-unsafe and all, but i really need the user to be
> able to put any kind of data into it. If you got another idea, please let me
> know.

Could i suggest that you add a type variable to hwnd to make it
type-safe? eg: instead of

type hwnd = ... 
type 'a hwnd = ...

so the functions would have the types:
external set_window_data : wnd:'a hwnd -> 'a -> unit = "set_window_data"
external get_window_data : wnd:'a hwnd -> 'a = "get_window_data"

If hwnd is a pre-existing type (as i suspect it is) then you could
create a type alias that uses phantom types (as far as i understand
them, at least), ie:

type 'a my_hwnd = hwnd

This way you will ensure the same type of data is set / got from the

> BTW, then with my handle i'm doing that :
>     (set_window_data wnd 0);
>     (print_int (get_window_data wnd));
> but it raises (about the call the get_window_data ) :
>     This expression has type wnd:hwnd -> 'a but is here used with type int

This is an oddity with the use of compulsary labelled arguments, as
introduced in ocaml 3.04. You need to specify the label of the argument
passed, ie:

set_window_data ~wnd:wnd 0; 
print_int (get_window_data ~wnd:wnd);

Hope this helps..
Ceri Storey <>
vi(1)! postfix(7)! pie(5)!
Bug reports:  FAQ:
To unsubscribe, mail  Archives: