rescript-lang / rescript-lang/rescript

Better output for Tagged Variants with non-inline record

Offen
#8,280 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
OCaml
Sterne
7.5k
Forks
485
Ø Merge
1 T. 2 Std.
Gemergte PRs (30 T.)
55

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue enthält ein Beispiel für eine rekursive Tagged-Variant sowie die aktuelle und die gewünschte JavaScript-Ausgabe; reproduziere zunächst dieses Beispiel und verfolge den Compiler-Pfad, der Datensätze für Tagged-Variants ausgibt. Ermittle die geltenden Einschränkungen für Tag/Feldnamen und einzelne Datensätze und überprüfe anschließend, dass das Beispiel abgeflachte Felder ohne den _0-Wrapper erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, ocaml
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.