rescript-lang / rescript-lang/rescript

Better output for Tagged Variants with non-inline record

Aperta
#8,280 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
OCaml
Stelle
7.5k
Fork
485
Merge medio
1g 2h
PR unite (30g)
55

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

La issue fornisce un esempio di tagged-variant ricorsiva e l’output JavaScript attuale rispetto a quello desiderato; inizia riproducendo quell’esempio e tracciando il percorso del compilatore che emette i record tagged-variant. Stabilisci i vincoli applicabili per tag/nome del campo e per il record singolo, quindi verifica che l’esempio produca campi appiattiti senza il wrapper _0.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, ocaml
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.