ahrefs/atd

Generalize the option to always output optional fields that are set to the default value (`~field : ...` syntax)

mjambon opened this issue · 0 comments

To reduce breakage when an optional record field becomes required, an implementation of an ATD interface can output optional fields even though they have the default value. It allows future implementations that read old data or data produced by an old implementation in which the field used to be optional.

This applies to the ~ syntax for optional fields with a default but not to the ? syntax which are optional fields without a default or equivalently whose default is null/None. The main use of ~ is for lists and booleans where the default is obvious:

type t = {
  ~items: item list;
  ~enable_foo: bool;
}

A valid JSON representation for a record of type t is {}. It has the advantage of being compact. Forcing the output of default values would instead produce the following JSON object:

{
  "items": [],
  "enable_foo": false
}

Such an option is already supported by atdgen in the form of -j-defaults. There is demand for having it across all backends (especially Python and TypeScript). Alternatively, this can be made the default or the only behavior.