[@bs.deriving abstract] vs. modules


#1

This code works great:

[@bs.deriving abstract] type resultType = {
  data: array(array(string)),
  error: string
};

let z: resultType = resultType(~data=[|[|"a", "b"|]|], ~error="none");
Js.log(z);
let w = data(z);
Js.log(w);

But this code:

module Results = {
  [@bs.deriving abstract]  type t = {
      data: array(array(string)),
      error: string
  };
};


let x: Results.t = Results.t(~data=[|[|"a", "b"|]|], ~error="none");
Js.log(x);
let y = Results.t.data(x);

tells me The record field data can't be found.. What am I doing wrong here?


#2

Results.t is the type, to get the accessor you have to use Results.data (no t):

let y = Results.data(x);

#3

For some details, bs.deriving generates functions for you to manipulate this record-looking thing (which is actually a JS object underneath). It’ll generate a constructor function with the same name as the type, getter functions for all the fields and setters for all the mutable fields. The getters and setters’ names are fieldName and fieldNameSet.
All these functions are available in the scope where the type is defined.