Run base constructor then blocks on the derived instance

Aperta
#5,272 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
javascript, python, typescript
Ambito
compilers

Direzione di ricerca

Read the constructor inheritance handling and the tests from the earlier #5235 branch at 2a69b6a8f; those tests identify the shapes a fix must handle. Reproduce the example on .NET and the #5235 branch, then trace how explicit constructors and base then blocks are compiled. Done means both derived classes compile and produce true and 2, while the listed edge cases remain correct.

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

Descrizione

bug

On JS, TS and Python, Fable can't inherit from a class whose explicit constructor has a self identifier, and a class with a base class can't have one. Once #5235 merges, both are compile errors, as they are on main. .NET prints true and 2.

let saved = ResizeArray<obj>()

type Base =
    val Name: string
    new (name: string) as self = { Name = name } then saved.Add(box self)

type Derived =
    inherit Base
    val X: int
    new () = { inherit Base("d"); X = 1 }

type Counter =
    val mutable Count: int
    new (c: int) as self = { Count = c } then self.Bump()
    member this.Bump() = this.Count <- this.Count + 1

type CounterChild =
    inherit Counter
    val X: int
    new () = { inherit Counter(1); X = 1 }

let d = Derived()
printfn "%b" (obj.ReferenceEquals(saved.[0], d))
printfn "%d" (CounterChild()).Count

On the #5235 branch, both derived classes fail with:

error FABLE: Program.Base has a constructor with a self identifier (`new() as x`), which Fable does not support inheriting from.

Both programs are valid F#. Supporting them means running the base then block on the derived instance. One idea, untested, is to split explicit constructors into separate allocate and initialize steps.

An earlier version of #5235 built a separate base object and copied its fields into the derived one. That was dropped because it gave wrong output for: curried function fields in the derived class, a base behind a .fsi, an [<AttachMembers>] base, and leaking self. It's in the branch history at 2a69b6a8f, and the tests from that version list the shapes a real fix has to handle.

Lingua principale
F#
Stelle
3.2k
Fork
335
Merge medio
18h 57m
PR unite (30g)
154

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

  • Nessun Dockerfile né file Docker Compose
  • Nessun modello di pull request
  • Nessuna 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.

Altre issue di fable-compiler/Fable

Tutte le issue di fable-compiler/Fable

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.