rescript-lang / rescript-lang/rescript

Better output for Tagged Variants with non-inline record

Open
#8,280 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
OCaml
Stars
7.5k
Forks
485
Avg merge
1d 2h
Merged PRs (30d)
55

Description

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
};

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides a recursive tagged-variant example and current versus wanted JavaScript output; start by reproducing that example and tracing the compiler path that emits tagged variant records. Establish the applicable tag/field-name and single-record constraints, then verify that the example produces flattened fields without the _0 wrapper.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, ocaml
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.