Run base constructor then blocks on the derived instance

Open
#5,272 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Domain
compilers

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
F#
Stars
3.2k
Forks
335
Avg merge
18h 57m
Merged PRs (30d)
154

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • No Dockerfile or Docker Compose file
  • No pull request template
  • No 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.

More from fable-compiler/Fable

All issues in fable-compiler/Fable

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.