Skip to content

Constructor overloads: several constructor implementations, picked by new - #696

Merged
ASDAlexander77 merged 1 commit into
mainfrom
constructor-overloads
Oct 26, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
constructor-overloads

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

A class can now implement more than one constructor:

class Array<T> {
    constructor(private data: T[] = []) {}
    constructor(count: int = 0) { this.data = []; this.data.length = count; }
}
new Array<int>(10);      // the second
new Array<int>([1, 2]);  // the first
new Array<int>();        // a tie: the first declared

Before this, the second constructor failed in one of two ways:

  • With parameter properties: can't resolve name: data. generateConstructorStatements put the this.x = x assignments of every constructor's parameter properties into each one.
  • Otherwise: redefinition of symbol named 'B.constructor'.

Changes

  • A constructor assigns only its own parameter properties. A field that two constructors both declare is added once.
  • The first constructor keeps the name constructor, so super calls, generated default constructors and casts still find it. Any other constructor is renamed constructor#N, as @cpp overloads are (mlirGenClassConstructorOverloadNames).
  • new picks the constructor its arguments fit best, from their types before the arguments are generated (resolveConstructorOverload). An array literal is generated for the chosen constructor's T[] parameter, not the first one's. The scoring that resolveCppOverload used is now shared in overloadArgumentFit.

Not covered (these still use the first constructor)

  • super(...) to a base class with overloads
  • the constructor generated for a subclass that has none
  • @value classes
  • inferring type arguments from the constructor's arguments

Tests

  • New: 00class_constructor_overloads.ts. Registered for compile and JIT and in the rc/none corpus.
  • Passes under gc, rc and none. Under -mm=own it fails, but a generic class with only one constructor fails the same way.
  • 68 related existing tests pass under the JIT with --no-default-lib: classes, overloads, @cpp, new, super.
  • The full compiler test runner has not been run.

The TypeScriptCompilerDefaultLib branch array-count-constructor depends on this PR.

🤖 Generated with Claude Code

… new

A class may implement more than one constructor. The first stays
`constructor`; any other is renamed `constructor#N`, as @cpp overloads
are, and `new` picks the one its arguments fit best, by their types
before they are generated. A tie goes to the one declared first.

A constructor now assigns only its own parameter properties: the
assignments of every constructor were put into each, so
`constructor(count: int)` got `this.data = data` ("can't resolve name").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 5a2a702 into main Oct 26, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the constructor-overloads branch October 26, 2026 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant