Explicit constructor inheriting a class with a primary constructor drops the base

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

I maintainer di solito rispondono entro 1 giorno

@MangelMaxime ci sta già lavorando.

Dal 2/11/2026.

  • #5247 di @MangelMaxime — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
44/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
fsharp, javascript, python
Ambito
compilers

Direzione di ricerca

Start by running the issue’s F# reproduction and inspecting the generated JavaScript; compare the result with the reported Python behavior. Trace how the compiler handles explicit constructors and inherited primary-constructor arguments, using the linked #5226 context as background. Done means derived instances retain the base behavior across targets, and unsupported inheritance chains produce a compile error instead of incorrect output.

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

Descrizione

bug

A class written with val fields and an explicit new that inherits from a class with a primary constructor compiles to a class that does not extend the base. The base constructor is called but its result is thrown away, so inherited members read undefined and type tests against the base fail.

type Point(x: int) =
    member _.X = x

type Labelled =
    inherit Point
    val Label: string
    new (x: int, label: string) = { inherit Point(x); Label = label }

let p = Labelled(3, "origin")
printfn "X: %A, Label: %A, isPoint: %b" p.X p.Label (box p :? Point)

.NET prints X: 3, Label: "origin", isPoint: true. Fable JS prints X: 0, Label: "origin", isPoint: false. Python behaves the same way.

Generated JS:

export class Labelled {
    constructor(Label) {
        this.Label = Label;
    }
}
export function Labelled_$ctor_Z176EF219(x, label) {
    Point_$ctor_Z524259A4(x);
    return new Labelled(label);
}

This is the case #5226 left out. That PR fixed #2425 for the situation where every class in the chain uses val fields: the generated constructor of the derived class takes the inherited fields and the explicit constructor copies them from an instance built by the base's own constructor function. That does not work here because Point's JS constructor takes its constructor arguments, not its fields, so there is nothing to copy from. The fix probably has to pass the inherit Point(x) arguments straight through to super(...), which also means a val class two levels below a primary-constructor class cannot be supported that way and should get a compile error rather than silent wrong output.

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

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.