Skip to content

Better output for Tagged Variants with non-inline record #8280

Open

Description

@Freddy03h

Inline records in Variant provide good JS output, which is especially useful for bindings. When combined with tagged variants, they also provide a better DX.

However, it’s not always possible to inline records (or spread them in the case of recursive types), so I’m wondering if it would be possible to improve the JS output in the case of tagged variants.

One possible constraint would be to verify that the tag does not conflict with any of the record fields. We could also keep the same constraint as inline records: only one record.

@tag("nodeType")
type rec node =
  | @as(1) Element(element)
  | @as(3) Text(text)
  | @as(9) Document(document)
and element = {
  childNodes: array<node>,
  ownerDocument: null<document>,
}
and text = {
  nextElementSibling: null<element>,
}
and document = {
  children: array<element>,
}

let anElement = Element({
  childNodes: [Text({nextElementSibling: Null.null})],
  ownerDocument: Null.null,
})

Current output:

let anElement = {
  nodeType: 1,
  _0: {
    childNodes: [{
        nodeType: 3,
        _0: {
          nextElementSibling: null
        }
      }],
    ownerDocument: null
  }
};

Wanted output:

let anElement = {
  nodeType: 1,
  childNodes: [{
    nodeType: 3,
    nextElementSibling: null
  }],
  ownerDocument: null
};

Activity

  1. changed the title [-]Better output for Tagged Variants[/-] [+]Better output for Tagged Variants with non-inline record[/+] on Mar 6, 2026
  2. fhammerschmidt commented on Mar 10, 2026

    @fhammerschmidt
    Member

    @cristianoc codex told me this is feasible, but what's your opinion?

  3. cristianoc commented on Mar 10, 2026

    @cristianoc
    Collaborator

    Construction and deconstruction Element(x) is not so clear. It would need to perhaps make copy, perhaps breaking identity.

  4. cometkim commented on Jul 16, 2026

    @cometkim
    Member

    See also the earlier discussion: #6547 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions