How to create a ReScript object with ppxlib?

I’m finishing rescript-schema-ppx and got a problem with ReScript object support. When I create it using the following code:

Exp.object_
  (Cstr.mk
    (Pat.var (mkloc "this" Location.none))
    [
      Cf.method_
        (mkloc "field" Location.none)
        Public
        (Cfk_concrete (Fresh, [%expr s.unknown]));
    ])]))

I get the OCaml style objects are not supported from the ReScript compiler. How can I create an object value which will work in ReScript?

eg:

let simpleObject = 
// I need to create this ⬇️
{
  "foo": "Hi",
  "bar": 123,
}
let o = {"x": 3}

the internal representation looks quite involved:

% ./bsc -dparsetree tst.res
[
  structure_item 
    Pstr_value Nonrec
    [
      <def>
        pattern 
          Ppat_var "o" 
        expression 
          Pexp_letmodule "J" 
          module_expr 
            Pmod_structure
            [
              structure_item 
                Pstr_primitive
                value_description "unsafe_expr"  
                  core_type 
                    Ptyp_arrow
                    Labelled "x"
                    core_type 
                      Ptyp_var a0
                    core_type 
                      Ptyp_object Closed
                        method x
                          core_type 
                            Ptyp_var a0
                  [
                    ""
                    "????

                        ???A?!x@"
                  ]
            ]
          expression 
            Pexp_apply
            expression 
              Pexp_ident "J.unsafe_expr" 
            [
              <arg>
              Labelled "x"
                expression 
                  Pexp_constant PConst_int (3,None)
            ]
    ]
]

Not sure what the internal module J does.

This looks simpler, before the internal compiler ppx acts on it. Worth a try.

% ./bsc -bs-no-builtin-ppx -dparsetree tst.res 
[
  structure_item 
    Pstr_value Nonrec
    [
      <def>
        pattern 
          Ppat_var "o" 
        expression 
          Pexp_extension "obj"
          [
            structure_item 
              Pstr_eval
              expression 
                Pexp_record
                [
                  "x" 
                    expression 
                      Pexp_constant PConst_int (3,None)
                ]
                None
          ]
    ]
]
2 Likes

Thank you very much. This looks much better than the first one :sweat_smile:

1 Like