Skip to content

binding: let applicable methods of derived types hide base methods - #54

Merged
rameel merged 1 commit into
mainfrom
binding/derived-method-overloads
Nov 21, 2026
Merged

rameel merged 1 commit into
mainfrom
binding/derived-method-overloads

Conversation

@rameel

@rameel rameel commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Overload resolution chose the most specific of all applicable methods, wherever they were declared. With BaseClass.M(int) and DerivedClass.M(long), Cls.M(1) called BaseClass.M(int), while C# calls DerivedClass.M(long). The same applied to indexers (this[int] in the base class, this[long] in the derived one) and to methods of base interfaces. No error was reported: another method was called silently.

This PR applies the C# rule from §12.8.10.2: before the best method is chosen, an applicable method is removed if it is declared in a base type of the type that declares another applicable method.

  • An override belongs to the type that introduced the method, so DerivedClass.M(long) hides BaseClass.M(int) even if DerivedClass also overrides M(int). The same holds for a covariant return override, which reflection reports as a new method of the derived class. It is chosen over the method it overrides, so the call gets the override's return type.
  • Base types are found by inheritance, not assignability: for a contravariant IConsumer<in T>, the methods of IConsumer<object> and IConsumer<string> do not hide each other.
  • A method of a class other than Object removes the methods of statically imported interfaces. An interface implemented by a struct is not its base type, so its methods stay.

The hierarchy depth no longer decides between methods with the same parameters. The same method imported from two unrelated types is now reported as an ambiguous match, like CS0121 in C#, instead of resolving to the type with the deeper hierarchy.

This changes the chosen overload in existing expressions when a derived type declares an overload that matches the arguments worse than a method of its base type.

Overload resolution chose the most specific of all applicable methods,
wherever they were declared. With BaseClass.M(int) and
DerivedClass.M(long), Cls.M(1) called BaseClass.M(int), and the same
happened with indexers and methods of base interfaces. C# calls
DerivedClass.M(long): it removes the applicable methods declared in a
base type of the type that declares another applicable method before
choosing the best one.

Apply this rule in MethodResolver. As in C#, an override belongs to
the type that introduced the method. This includes a covariant return
override, which reflection reports as a new method of the derived
class and which is chosen over the method it overrides. Base types are
found by inheritance rather than assignability, so instantiations of a
variant interface do not hide each other. A method of a class other
than Object also removes the methods of statically imported
interfaces.

The hierarchy depth no longer decides between methods with the same
parameters. The same method imported from two unrelated types is now
an ambiguous match, as in C#, instead of resolving to the type with
the deeper hierarchy.
@rameel
rameel merged commit 1e0dfde into main Nov 21, 2026
1 check passed
@rameel
rameel deleted the binding/derived-method-overloads branch November 21, 2026 18:53
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