# \[ANN\] living 0.1.0

**URL:** <https://discuss.ocaml.org/t/ann-living-0-1-0/14964>\
**Category:** Ecosystem\
**Tags:** ctypes, ffi\
**Created:** [July 13, 2024, 1:48pm UTC](https://discuss.ocaml.org/t/ann-living-0-1-0/14964 "2024-07-13T13:48:42Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Fizzixnerd](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fizzixnerd/32/5322_2.png) [@Fizzixnerd](https://discuss.ocaml.org/u/Fizzixnerd)\
**Post date:** [July 13, 2024, 1:48pm UTC](https://discuss.ocaml.org/t/ann-living-0-1-0/14964/1 "2024-07-13T13:48:42Z")

</div>

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

```ocaml
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:

```ocaml
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!

> **[GitHub - Fizzixnerd/ocaml-living: A safer FFI interface for OCaml](https://github.com/Fizzixnerd/ocaml-living)**
>
> A safer FFI interface for OCaml. Contribute to Fizzixnerd/ocaml-living development by creating an account on GitHub.

---

_[View the full topic](https://discuss.ocaml.org/t/ann-living-0-1-0/14964)._
