Repository navigation
Constructor overloads: several constructor implementations, picked by new - #696
Merged
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A class can now implement more than one constructor:
Before this, the second constructor failed in one of two ways:
can't resolve name: data.generateConstructorStatementsput thethis.x = xassignments of every constructor's parameter properties into each one.redefinition of symbol named 'B.constructor'.Changes
constructor, so super calls, generated default constructors and casts still find it. Any other constructor is renamedconstructor#N, as@cppoverloads are (mlirGenClassConstructorOverloadNames).newpicks the constructor its arguments fit best, from their types before the arguments are generated (resolveConstructorOverload). An array literal is generated for the chosen constructor'sT[]parameter, not the first one's. The scoring thatresolveCppOverloadused is now shared inoverloadArgumentFit.Not covered (these still use the first constructor)
super(...)to a base class with overloads@valueclassesTests
00class_constructor_overloads.ts. Registered for compile and JIT and in the rc/none corpus.-mm=ownit fails, but a generic class with only one constructor fails the same way.--no-default-lib: classes, overloads,@cpp,new,super.The TypeScriptCompilerDefaultLib branch
array-count-constructordepends on this PR.🤖 Generated with Claude Code