caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: Goswin von Brederlow <goswin-v-b@web.de>,
	Joel Reymont <joelr1@gmail.com>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] do i need a private row type?
Date: Tue, 17 May 2011 06:26:21 +0200	[thread overview]
Message-ID: <877h9qt0k2.fsf@frosties.localnet> (raw)
In-Reply-To: <BANLkTik79B8+OgTHvieYDzL_DKs4su68wg@mail.gmail.com> (Gabriel Scherer's message of "Mon, 16 May 2011 09:07:25 +0200")

Gabriel Scherer <gabriel.scherer@gmail.com> writes:

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

This is still type safe given sufficient encapsulation in a module so it
can't be abused. But as I said, it is the dirty solution, like anything
requireing Obj.magic is. And you are right, if all you have is one
callback then a simple closure is better.

But you also need access to the Unix.file_descr underlying the socket to
select on it. And then you need a second callback to handle error
conditions. And then your socket probably has some state and things
become mutable and closures quickly become ugly and use up a lot more
memory if you have many sockets.

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

I haven't tried this with first-class modules and the better support for
abstract types yet. But I would be interested to see in how they improve
on the 2 records solution.

Another thing I didn't mention is to use objects. This use case is
actually the poster child for objects with virtual functions.

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

MfG
        Goswin

  parent reply	other threads:[~2011-05-17  4:26 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-15 13:56 Joel Reymont
2011-05-16  6:24 ` Goswin von Brederlow
2011-05-16  7:07   ` Gabriel Scherer
2011-05-16  8:57     ` Joel Reymont
2011-05-16  9:31     ` Joel Reymont
2011-05-16  9:58     ` Joel Reymont
2011-05-16 10:05     ` Joel Reymont
2011-05-16 12:02     ` Joel Reymont
2011-05-16 12:32     ` Joel Reymont
2011-05-16 13:02     ` Joel Reymont
2011-05-16 13:39       ` Gabriel Scherer
2011-05-16 13:46         ` Joel Reymont
2011-05-16 14:02         ` Joel Reymont
2011-05-16 14:06           ` Gabriel Scherer
2011-05-16 14:08             ` Joel Reymont
2011-05-17  4:26     ` Goswin von Brederlow [this message]
2011-05-19  4:15 ` Pedro Borges
2011-05-19  7:33   ` Joel Reymont

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877h9qt0k2.fsf@frosties.localnet \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    --cc=joelr1@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).