On Mon, May 16, 2011 at 8:24 AM, Goswin von Brederlow <goswin-v-b@web.de> wrote:
First thing is that if you have different 'a poll_items then you need to
include a callback that accepts a matching 'a Socket.t. E.g.:

type 'a poll_item = ('a Socket.t * event_mask * ('a Socket.t -> event_mask -> unit))

Then you can make an unit poll_item array and use

let set i (item : 'a poll_item) =
   array.[i] <- Obj.magic item

let call i =
   let (sock, mask, fn) = array.[i]
   in
   fn sock mask

The important part here is that you ensure the 'a Socket.t is only
accessed through the callback. Only that will have the right type.

If "call" is the only thing you wish to do on your array, you don't need such an ugly solution. "sock" and "mask" being constants for a given item, you can simply capture them in a closure.

  type poll_item_thunk = unit -> unit

  let set i (socket, event_mask, callback) =
    array.(i) <- (fun () -> callback socket event_mask);

  let call i = array.(i) ()


There is no reasonable answer to "how do I force OCaml into breaking type safety ?". Joel must have an use case in mind which is actually type-safe; maybe it's only the callback as you demonstrated (I fail to see its usefulness, but I don't know anything about ZeroMQ), maybe it's something a bit more complicated. But we won't be able to give him a satisfying solution unless he describes more precisely what this type-safe use is.

The most general solution, as you alluded, is to use existential types. This can be done by an encoding into universal types (this is the "2 records" approach), or using first-class modules and abstract types.

Joel, may you be more explicit as to what you want to do with those "poll_item" array. More precisely, how do you plan to *use* the data in the array?

Joel Reymont <joelr1@gmail.com> writes:

> https://github.com/wagerlabs/ocaml-zmq/blob/master/src/ZMQ.ml
>
> Looking at the Poll module at the bottom and the definition of poll_item as
>
>   type 'a poll_item = ('a Socket.t * event_mask)
>
> I would like to be able to put different 'a in the same poll_item array.
>
> This is not possible as written, e.g.
>
> Error: This expression has type
>          ZMQ.Socket.pub ZMQ.Socket.t * ZMQ.Poll.event_mask
>        but an expression was expected of type
>          ZMQ.Socket.pull ZMQ.Socket.t * ZMQ.Poll.event_mask
>
> How do I type poll_item and poll_item array so that the array is polymorphic with respect to 'a?
>
> Does the definition of Socket a the top of ZMQ.ml need to be changed to make this possible?
>
>       Thanks, Joel

How clean do you want this to be? Lets go with easy and dirty.

First thing is that if you have different 'a poll_items then you need to
include a callback that accepts a matching 'a Socket.t. E.g.:

type 'a poll_item = ('a Socket.t * event_mask * ('a Socket.t -> event_mask -> unit))

Then you can make an unit poll_item array and use

let set i (item : 'a poll_item) =
   array.[i] <- Obj.magic item

let call i =
   let (sock, mask, fn) = array.[i]
   in
   fn sock mask

The important part here is that you ensure the 'a Socket.t is only
accessed through the callback. Only that will have the right type.


If you want to make it clean then you need a 2 layer approach with 2
records and 'a. 'a -> 'b style types. I can't remember how to do this
from memory but search the list archive for something like universal
container. I've asked the same a few years back.

MfG
       Goswin

--
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs