caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Philippe Veber <philippe.veber@gmail.com>
To: Alain Frisch <alain@frisch.fr>
Cc: Gabriel Scherer <gabriel.scherer@gmail.com>,
	Leo P White <lpw25@cam.ac.uk>, caml <caml-list@inria.fr>
Subject: Re: [Caml-list] [ANN] ocamlopen 1.0.2
Date: Tue, 17 Apr 2012 12:02:52 +0200	[thread overview]
Message-ID: <CAOOOohRJTYm2xWFHsC6wWJ-Ngkk+GJ5YnLYyit2u0iQ4DA64qA@mail.gmail.com> (raw)
In-Reply-To: <4F86E6BC.7070706@frisch.fr>

[-- Attachment #1: Type: text/plain, Size: 1682 bytes --]

2012/4/12 Alain Frisch <alain@frisch.fr>

> On 04/12/2012 04:16 PM, Philippe Veber wrote:
>
>> Isn't this a good use case for polymorphic variants too ?
>>
>
> I don't see how to use polymorphic variants here.  The message bus itself
> need to provide functions like:
>
>  val dispatch: message -> unit
>  val register_listener: (message -> unit) -> unit
>
>
> How do you define the message type without having access to all possible
> message constructors?

I reckon you can't, but I mistakenly thought you could at least define the
bus without writing the full message type:

module Bus : sig
  type 'a t
  val create : unit -> 'a t
  val dispatch : 'a t -> 'a -> unit
  val register : 'a t -> ('a -> unit) -> unit
end
=
struct
  type 'a t = ('a -> unit) list ref
  let create () = ref []
  let dispatch bus msg =
    List.iter (fun f -> f msg) !bus
  let register bus f =
    bus := f :: !bus
end


let bus = Bus.create ()

let () = Bus.register bus (function `a -> print_char 'a')
let () = Bus.register bus (function `b n -> print_int n)

However this is not a legal program:

  let () = Bus.register bus (function `b n -> print_int n);;
                                      ^^^^
Error: This pattern matches values of type [< `b of 'a ]
       but a pattern was expected which matches values of type [< `a ]
       These two variant types have no intersection

Well, I mentionned polymorphic variants because you can at least define
listeners without knowing the full message type and even reuse them for
buses with different message types. So you have a certain flexibility,
compared to monomorphic types.

But ok, maybe the remark wasn't even worth 2 cents :o).

Cheers,
ph.

[-- Attachment #2: Type: text/html, Size: 4493 bytes --]

  reply	other threads:[~2012-04-17 10:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-11 21:13 Leo P White
2012-04-12 12:56 ` Gabriel Scherer
2012-04-12 14:03   ` Alain Frisch
2012-04-12 14:16     ` Philippe Veber
2012-04-12 14:29       ` Alain Frisch
2012-04-17 10:02         ` Philippe Veber [this message]
2012-04-12 17:07   ` Leo P White
2012-04-12 17:21     ` Alain Frisch
2012-04-12 20:31       ` Leo P White

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=CAOOOohRJTYm2xWFHsC6wWJ-Ngkk+GJ5YnLYyit2u0iQ4DA64qA@mail.gmail.com \
    --to=philippe.veber@gmail.com \
    --cc=alain@frisch.fr \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    --cc=lpw25@cam.ac.uk \
    /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).