Should we not use bytes in OCaml?

Hello Everyone!

I’m rather new in OCaml, my last language I used a lot in my day to day activities professional and not was Golang.

In Golang the slice of bytes is essential, I get the feeling that in OCaml they want us to stay away from Bytes, it seems to me as a rather newcomer that there’s very little functions or apparent ways of working with or handling bytes in OCaml.

Is this a wrong feeling I’ve gotten and if so how do you guys usually handle (IO) input and output?

Thanks!

1 Like

In Go the common way to do it is using io.Reader and io.Writer. They expose the low-level read/write functionality in terms of byte slices. In OCaml I would say the common way to do I/O is buffered I/O using channels. See specifically these modules in the standard library:

There are also lots of I/O operations available from the Stdlib module (the one that’s always in scope): OCaml library : Stdlib

The common way to work with these I/O operations is reading/writing strings, but you can also use the available functions that deal with bytes directly. A typical example is:

val input : in_channel -> bytes -> int -> int -> int

Thinking about it a bit more, you might benefit from reading or skimming through some chapters of the OCaml Unix programming book, which deals with I/O and related topics: Unix system programming in OCaml

3 Likes