Skip to content

parser: pass default values for omitted optional arguments - #21

Merged
rameel merged 2 commits into
mainfrom
parser/optional-arguments
Nov 18, 2026
Merged

rameel merged 2 commits into
mainfrom
parser/optional-arguments

Conversation

@rameel

@rameel rameel commented Nov 18, 2026

Copy link
Copy Markdown
Owner

A call that omitted optional arguments or passed no arguments for a params parameter failed with Incorrect number of arguments supplied for call to method, although the method resolver had accepted it. For example, string.Format("x"), "a,b".Split(","), and "a,b".Split(',') failed this way.

This PR passes an empty array for a parameter array without arguments and the default value for an omitted optional parameter, as C# does. A parameter declared with [Optional] and without a default value gets Type.Missing if its type is object, and the default value of its type otherwise.

The method resolver also chose between such overloads differently from C#: it preferred the overload with more parameters, so M(1) would call M(int, int = 0) instead of M(int). It now prefers the overload whose parameters all get arguments and reports overloads that both need default arguments, such as M(int, int = 0) and M(int, int = 0, int = 0), as ambiguous.

When the parameter types did not decide between two overloads, the
method resolver preferred the one with more parameters, as the
reflection binder does. As a result, 'M(1)' selected M(int, int = 0)
instead of M(int), and overloads that both need default arguments,
such as M(int, int = 0) and M(int, int = 0, int = 0), were not
reported as ambiguous.

Prefer the overload whose parameters all get arguments, as C# does,
and report overloads that both need default arguments as ambiguous.
When both overloads are applicable only in their expanded form, the
one with more declared parameters still wins.
A call that omitted optional arguments or passed no arguments for a
parameter array failed with "Incorrect number of arguments supplied
for call to method", although the method resolver had accepted it.
For example, 'string.Format("x")', '"a,b".Split(",")', and
'"a,b".Split(',')' failed this way.

Pass an empty array for a parameter array without arguments and the
default value for an omitted optional parameter, as C# does. A
parameter declared with [Optional] and without a default value gets
Type.Missing if its type is object, and the default value of its type
otherwise.
@rameel
rameel force-pushed the parser/optional-arguments branch from d503313 to 565ae48 Compare November 18, 2026 08:18
@rameel
rameel merged commit 37fce0a into main Nov 18, 2026
1 check passed
@rameel
rameel deleted the parser/optional-arguments branch November 18, 2026 08:18
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