caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Type hidding
       [not found] <000901c19ecf$b8c93ee0$0b01a8c0@mit.edu>
@ 2002-01-17 17:44 ` Warp
  2002-01-18  6:40   ` Jacques Garrigue
  0 siblings, 1 reply; 5+ messages in thread
From: Warp @ 2002-01-17 17:44 UTC (permalink / raw)
  To: Jeff Henrikson; +Cc: OCaml

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

What's wrong ?

Warp

> Do you know about Obj.magic?  It unsafely casts anything to the unlimited
polymorphic type, so you can cast to something else:
>
> (Obj.magic x : int)
>
> casts x to an int no matter what.  Note that this does not help you at all
if you are using the least significant bit, which is
> reserved for pointer tracing in ocaml.  If you bind bind something
arbitrary with a 0 lsb to an ocaml variable, you run the risk of
> GC running off the end of the earth tracing that.  You are supposed to
convert the variable to an int32 or something else in C
> before returning it to caml.  See the FFI docs.
>
>
> Jeff
>
> > Hello !
> > I'm having theses functions :
> >
> > 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.
> >
> > 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
> >
> > I've tried to "force" the return value to int but it doesn't seems to
work.
> > Any help welcomed.
> >
> > Warp
> >
> > -------------------
> > 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
> >
>
>

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Type hidding
  2002-01-17 17:44 ` [Caml-list] Type hidding Warp
@ 2002-01-18  6:40   ` Jacques Garrigue
  0 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2002-01-18  6:40 UTC (permalink / raw)
  To: warplayer; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Type hidding
  2002-01-16 20:09 Warp
  2002-01-16 23:32 ` Ceri Storey
@ 2002-01-17 10:57 ` Daniel de Rauglaudre
  1 sibling, 0 replies; 5+ messages in thread
From: Daniel de Rauglaudre @ 2002-01-17 10:57 UTC (permalink / raw)
  To: caml-list

Hi,

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.

If you don't want to give type parameters to hwnd, like suggested, and
if the stored values are monomorphic, you can use this system of
monomorphic dynamics.

The following function creates a system of dynamics, which are values
of type unit -> unit. It returns a couple a two functions, one to
convert any monomorphic type into a dynamic, the other returning a
value from its corresponding dynamic:

   let make_dyn () =
     let r = ref None in
     let set x () = r := Some x in
     let get d =
       r := None;
       d ();
       match !r with
	 Some x -> x
       | None -> failwith "bad type"
     in
     set, get

Example:

   let set_int, get_int = make_dyn ()
   let set_string, get_string = make_dyn ()
   
   let x = set_int 32
   let y = set_string "abc"
   let z = set_int 8

   get_int x
   -> 32
   get_int z
   -> 8
   get_string y
   -> "abc"
   get_int y
   -> failed "bad type"

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Type hidding
  2002-01-16 20:09 Warp
@ 2002-01-16 23:32 ` Ceri Storey
  2002-01-17 10:57 ` Daniel de Rauglaudre
  1 sibling, 0 replies; 5+ messages in thread
From: Ceri Storey @ 2002-01-16 23:32 UTC (permalink / raw)
  To: Warp; +Cc: OCaml

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 = ... 
have:
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
window. 

> 
> 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 <cez@pkl.net> http://pkl.net/~cez/
vi(1)! postfix(7)! pie(5)!
-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Type hidding
@ 2002-01-16 20:09 Warp
  2002-01-16 23:32 ` Ceri Storey
  2002-01-17 10:57 ` Daniel de Rauglaudre
  0 siblings, 2 replies; 5+ messages in thread
From: Warp @ 2002-01-16 20:09 UTC (permalink / raw)
  To: OCaml

Hello !
I'm having theses functions :

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.

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

I've tried to "force" the return value to int but it doesn't seems to work.
Any help welcomed.

Warp

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-01-18  7:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <000901c19ecf$b8c93ee0$0b01a8c0@mit.edu>
2002-01-17 17:44 ` [Caml-list] Type hidding Warp
2002-01-18  6:40   ` Jacques Garrigue
2002-01-16 20:09 Warp
2002-01-16 23:32 ` Ceri Storey
2002-01-17 10:57 ` Daniel de Rauglaudre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).