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