[ocaml-ctypes] [Caml-list] ctypes - to string or to bigarray?

Jeremy Yallop yallop at gmail.com
Mon Feb 29 08:41:33 GMT 2016


On 28 February 2016 at 19:15, Malcolm Matalka <mmatalka at gmail.com> wrote:
> I'm writing bindings for a library that is just doing I/O and wondering
> what the suggested best practice is around using string or bigarray.  It
> seems like using Bigarray is probably the most versatile interface.
> People can decide to do the copying themselves or just use Bigarray
> everywhere.

Yes, Bigarray is the more versatile choice, and probably the better default.

> Are there any other things to consider?  Suggestions?

Since ctypes bindings are just OCaml programs, one possibility is to
abstract over the choice of storage.  David Sheets's libsodium
bindings are a good example of this approach; for example, here's a
functor which builds bindings for half a dozen functions based on an
abstract "Sodium_storage.S" signature:

    https://github.com/dsheets/ocaml-sodium/blob/95d26a85/lib_gen/sodium_bindings.ml#L64-L76

Elsewhere in the library are implementations of Sodium_storage.S for
Bigarray and for Bytes:

    https://github.com/dsheets/ocaml-sodium/blob/95d26a85/lib/sodium_storage.ml

One pleasant aspect of this approach is that the abstraction over
storage type doesn't cause any loss of type safety.  You can use all
the typed abstraction facilities of OCaml to construct your binding
descriptions, and the generated C code is checked against the headers
for the bound library by the C compiler.


More information about the Ctypes mailing list