A slight update to yesterday’s example
listener.ml supporting named callback functions:
open Cohml open Callback open Unix let _ = register "cbf_coh_insert" (fun k v -> print_endline ("Inserted key=" ^ k ^ " value=" ^ v)) let _ = register "cbf_coh_update" (fun k ov nv -> print_endline ("Updated key=" ^ k ^ " old value=" ^ ov ^ " new value=" ^ nv)) let _ = register "cbf_coh_delete" (fun k -> print_endline ("Deleted key=" ^ k)) let () = print_endline "Listening..."; let c = coh_getcache "test_cache" in coh_listen c "cbf_coh_insert" "cbf_coh_update" "cbf_coh_delete"; sleep 1000
I am not entirely comfortable with this as a problem in the names can’t be detected at compile time – tho’ at runtime there is a check that the callback functions do exist, it does not check their type signatures. I wish it was possible to pass a closure/let binding straight into C†. It would be possible to accidentally have the arguments out of order:
coh_listen c "cbf_coh_update" "cbf_coh_insert" "cbf_coh_delete";
And it will compile cleanly and crash as soon as it runs! We use advanced languages like OCaml precisely to avoid that possibility. But it does mean the ability to connect to more than one cache with MapListeners.
Unlike the Java and .NET versions, in C++ this mapping is performed at compilation time.
So non-trivial applications will have to include C++ to use custom objects. I’d hoped to present the users of my bindings with a pure OCaml API, but this should still be straightforward enough.
† Update: Apparently you can. Well, sort of. Hmm.