Description
BS7 record as object representation led to much easier bindings, but two use cases are not covered by this new representation:
- bindings to objects that need to be JSON-compatible (e.g. without any
undefined
fields) - bindings to objects with many optional fields (often used as configuration objects), that are too cumbersome to be created with the record creation syntax
Today, these two use cases are covered by the use of @bs.deriving abstract
or @bs.obj
annotations, but that makes the target type abstract, which removes the ability to use the very convenient features of records in OCaml like pattern matching.
Having a non-abstract @bs.deriving
annotation that would keep the type as record would bring the best of both worlds and would make @bs.deriving abstract
or @bs.obj
annotations almost useless.
An optional private
subannotation ([@bs.deriving private]
) would make the type private and would force the use of the generated creation function to create a record of this type (for JSON-compliant objects for example).
A safer approach would be to make the type private
by default and to provide a public
or non-private
subannotation to opt-out this effect.