Repository navigation
binding: let applicable methods of derived types hide base methods - #54
Merged
Merged
Conversation
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.
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.
Overload resolution chose the most specific of all applicable methods, wherever they were declared. With
BaseClass.M(int)andDerivedClass.M(long),Cls.M(1)calledBaseClass.M(int), while C# callsDerivedClass.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.
DerivedClass.M(long)hidesBaseClass.M(int)even ifDerivedClassalso overridesM(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.IConsumer<in T>, the methods ofIConsumer<object>andIConsumer<string>do not hide each other.Objectremoves 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.