I’m pleased to announce the first pre-opam version of the living
library, currently available only on GitHub for testing. I have some basic tests and a README explaining what it’s for, but basically, it prevents mistakes like
open Ctypes
(** Returns a pointer into the argument character string that points to the first
instance of the argument character. *)
let strchr : char ptr -> char -> char ptr =
Foreign.foreign "strchr" (ptr char @-> char @-> returning (ptr char))
let () =
let p = CArray.start (CArray.of_string "abc") in
let q = strchr p 'a' in
let () = Gc.compact () in
let c = !@ q in
if Char.(equal c 'a') then print_endline "yay!" else print_endline "boo!"
above from causing you pain. If you weren’t aware, the code above will almost always print “boo!”. Using living
, you can replace it with this code:
open Living
open Living_ctypes
let strchr : char ptr -> char -> char ptr Living_core.t =
let strchr_unsafe = Foreign.foreign "strchr" (ptr char @-> char @-> returning (ptr char)) in
fun s c -> Living_core.(strchr_unsafe s c => s)
let _ =
let open Living_core.Let_syntax in
let* p = CArray.start (CArray.of_string "abc") in
let* q = strchr p 'a' in
let () = Gc.compact () in
let* c = !@ q in
if Char.(equal c 'a') then print_endline "yay!" else print_endline "boo!"
Living_core.return ()
and it will always print “yay!”
Edit: should probably link to it!