How to make a covariant observable in OCaml

女生的网名这么多〃 提交于 2019-12-07 14:16:16

问题


I'm trying to make a wrapper for values that allows callers to register themselves for notifications about it. Here's some (working) code:

module Thing :
  sig
    type +'a t
    val make : 'a -> 'a t
    val watch : ('a -> unit) -> 'a t -> unit
    val notify : 'a t -> unit
  end = struct
    type 'a t = {
      obj : 'a;
      watchers : (unit -> unit) Queue.t
    }

    let make x = {
      obj = x;
      watchers = Queue.create ()
    }

    let watch fn x =
      x.watchers |> Queue.add (fun () -> fn x.obj) 

    let notify x =
      x.watchers |> Queue.iter (fun fn -> fn ())
  end

let () =
  let x = Thing.make (`Int 4) in
  Thing.watch (fun (`Int d) -> Printf.printf "Observed %d\n" d) x;
  let x = (x :> [`Int of int | `None] Thing.t) in
  Thing.notify x

However, this seems inefficient. Each queued watcher is a new closure with its own reference to the thing. It would make more sense to queue just the user's callback and add the x in notify, e.g.

  ... = struct
    type 'a t = {
      obj : 'a;
      watchers : ('a -> unit) Queue.t
    }

    let make x = {
      obj = x;
      watchers = Queue.create ()
    }

    let watch fn x =
      x.watchers |> Queue.add fn

    let notify x =
      x.watchers |> Queue.iter (fun fn -> fn x.obj)
  end

But having the 'a as part of the queue type means that 'a t is no longer covariant. I understand why this happens, but does anyone have a solution? i.e. how can I show OCaml that it's safe in this case?


回答1:


You can shift the place of capture:

module Thing :
  sig
    type +'a t
    val make : 'a -> 'a t
    val watch : ('a -> unit) -> 'a t -> unit
    val notify : 'a t -> unit
  end = struct
    type 'a t = {
      obj : 'a;
      watch : ('a -> unit) -> unit;
      notify : unit -> unit;
    }

    let make x =
      let queue = Queue.create () in
      let obj = x in
      let watch f = Queue.add f queue in
      let notify () = Queue.iter (fun f -> f x) queue in
      { obj; watch; notify; }

    let watch fn x = x.watch fn
    let notify x = x.notify ()
  end

If you want to feel really economical:

    let make x =
      let queue = Queue.create () in
      let obj = x in
      let rec watch f = Queue.add f queue
      and notify () = Queue.iter (fun f -> f x) queue in
      { obj; watch; notify; }


来源:https://stackoverflow.com/questions/21377727/how-to-make-a-covariant-observable-in-ocaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!