What's the best way to encode ADT with JSOO?


I’m trying to find the best way to encode ADT with js_of_ocaml. The main issue in to manage variants, indeed, translating records is straightforward. For example, the following OCaml type:

type a = { prop1: string; prop2: int }

could be transposed to the following JS class:

class type a = 
    method prop1 : Js.js_string Js.readonly_prop
    method prop2 : int Js.readonly_prop

However, to encode a variant type such as:

type b = A | B of int

I intuitively would like to use some inheritance principle to have some abstract class/interface b with two classed extending b. In JS this will look like:

class A extends b {
  constructor() { }

class B extends b {
  constructor(arg1) {
    this.arg1 = arg1

Unfortunatly, I didn’t found a way to do such a things with JSOO…
Did I miss something?

Another way could be to have:

class type b =
    method kind : Js.js_string Js.readonly_prop (* Expects "A" or "B" *)
    method args: Js.Unsafe.any Js.t Js.js_array Js.t Js.readonly_prop

Is there a more idiomatic way to achieve this?

I haven’t used jsoo but I have a (probably silly) question: doesn’t it already encode both records and variants for you? can’t you just use them directly?

Maybe the types and functions generated by origin-labs / ppx_deriving_jsoo · GitLab might be what you want?