Skip to content

An optional parameter fits an overload as the type it holds - #716

Merged
ASDAlexander77 merged 1 commit into
mainfrom
optional-param-overload-fit
Oct 28, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
optional-param-overload-fit

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

An optional parameter fits an overload as the type it holds

new Array<int>(10) (and, since #715, new Array(10)) against DefaultLib's Array<T>
constructors (count: int = 0) and (...args: T[]) made a one-element array: the rest
constructor won.

resolveConstructorOverload scores each constructor with overloadArgumentFit. The
defaulted count is typed !ts.optional<si32>, which an int argument reached only by
conversion (1), while the rest element int was an exact match (3). The optional is now
stripped before scoring, so both fit exactly and the tie goes to the constructor declared
first, (count: int).

overloadArgumentFit also picks among a @cpp class's overloaded methods; there too an
optional parameter now fits as the type it holds.

Tests

  • 00class_constructor_overloads.ts: C<T> with (count: int = 0) and (...args: T[]);
    new C<int>(10) ran the rest constructor before the fix.
  • ctest Release: 6082/6082 passed.
  • DefaultLib tests.ps1 (with the uncommitted Array<T> constructors): gc, rc, none, own
    pass in all four passes; new Array<int>(10) and new Array(10) have length 10.

Not in this PR

  • -mm=own: new Array<string>("a", "b") through the rest constructor fails the own
    checker (find/findLast/_with); new Array<string>(["a", "b"]) works.

🤖 Generated with Claude Code

`new Array<int>(10)` against DefaultLib's Array<T> constructors `(count: int = 0)`
and `(...args: T[])` went to the rest constructor (length 1): overloadArgumentFit
scored the defaulted parameter as optional<si32>, which an int reached only by
conversion (1), against the rest element's exact match (3). The optional is now
stripped before scoring, so both fit exactly and the one declared first wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 4cd87d5 into main Oct 28, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the optional-param-overload-fit branch October 28, 2026 15:05
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