Unbound value Bytes.set_int8

Error:

16 |   Bytes.set_int8 out_bytes 0 (v.x );
       ^^^^^^^^^^^^^^
Error: Unbound value Bytes.set_int8

Code:

open! Core


module Pt3 = struct
  type t =
    { x : int
    ; y : int
    ; z : int};;


let write (out_oc: Out_channel.t) (v: t) : unit =
  let out_bytes = Bytes.create 8 in 
  let _ = v in
  let _ = out_oc in
  let _ = out_bytes in
  Bytes.set_int8 out_bytes 0 (v.x );
  Out_channel.output out_oc out_bytes 0 1;Out_channel.bytes out_oc out_bytes 0 1;Out_channel.bytes out_oc out_bytes 0 1;  ();;


let read (in_ic: In_channel.t) (v: unit) : t =
  let _ = v in
  let _ = in_ic in 
  failwith "";;


end

set_int8 is documented at OCaml library : Bytes

val set_int8 : bytes -> int -> int -> unit
set_int8 b i v sets b's signed 8-bit integer starting at byte index i to v.

Context:

generating code that writes structs to bytearray

Looks like you’re using Core, not the standard OCaml libraries. Check the docs for Core, which has different signatures for many functions in the standard libraries.

2 Likes

On a related issue, is there a reason why OCaml library : Stdlib.Bytes seems to lack uint32 and uint64 ?

It has setters and getters for 32-bit and 64-bit little endian, big endian and native endian integers. According to the documentation “32-bit and 64-bit integers are represented by the int32 and int64 types, which can be interpreted either as signed or unsigned numbers.” If you convert Int64 to int on a 64 bit platform then you may lose the high bit, and likewise int32 on on 32-bit platforms. Int64.unsigned_to_int might also be useful to you.