-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Method overload resolution using pattern matching #1468
Comments
Relevant: dotnet/roslyn#955 |
The team might come to the same conclusion as they did with #581 (comment). |
I don't think this kind of syntax makes sense in a language like C#, which has full-blown method overloading and where method declarations can be fairly verbose. Method overloading is an issue, because your syntax makes it hard to distinguish separate overloads. As far as I know, languages that support this kind of syntax either don't have method overloading (like Haskell) or have only limited support for it (like Elixir). Verbosity of methods declarations, due to modifiers like I think a much better option in this case is a public override bool Equals(object obj) =>
obj switch
{
null => false,
MyStruct value => Equals(value),
_ => false
}; This is shorter than your version, and I'd say it's also easier to understand. |
This could be useful with partial classes and inheritance scenarios. |
If implemented I would prefer a more consistent syntax. Perhaps: public override bool Equals(object obj) when (obj is (object)null)
=> false;
public override bool Equals(object obj) when (obj is MyStruct value)
=> Equals(value);
public override bool Equals(object obj) => false; Or: public override bool Equals(object obj when obj is (object)null)
=> false;
public override bool Equals(object obj when obj is MyStruct value)
=> Equals(value);
public override bool Equals(object obj) => false; (I'm not specifically against |
I'd like to propose adding support for allowing a form of method overload resolution using pattern matching. The form this would take is similar to that in functional languages such as Elixir. It would essentially extend the pattern matching syntax supported in C# 7 for switch case statements to methods. I feel sure this must have been proposed before, but I can find no issue relating to it.
Example
Consider a simple implementation of equality for a struct:
This could be rewritten as:
Implementation
Conceptually, the way I would see this working is that the three versions of
Equals(object obj)
would be resolved to a single method along the lines ofAdvantages
I believe in many cases this would make code more readable and less verbose, in the same way that exception filtering and switch case pattern matching has.
It seems to me there would also be the opportunity for optimizations when a method is called with arguments whose values are known at compile time. For instance, consider the following:
Here the assignment to
bar
is via a call toProduct(,)
featuring compile-time constants which matches to an implementation returning a compile-time constant, so presumably the compiler could replace the call withvar bar = 1
. The example above is very trivial, but I could envisage in other usages this leading, via recursive calls, for instance, to compile-time function evaluation. That's either an advantage or opening the gates of hell, depending on your point-of-view.The text was updated successfully, but these errors were encountered: