-
Notifications
You must be signed in to change notification settings - Fork 4k
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
[Proposal] Operator overloading on Interfaces #5624
Comments
How would we define this operator? Typically they are in static methods in a class. Would you propose to allow static methods on interface definitions? |
I think operator overloading can be defined the same as extension methods, somewhere in a static class besides the interface declaration. |
Would this change also allow operator definitions for all types from extension methods? |
See also related discussion Static interface members Possibility to provide operators in interface Static Interfaces Proposal |
I don't see the operator overloading in the context of a static method inside the interface but that the operator overloading can resides somewhere not neccessary in the class/interface itself. |
@stefc Interfaces as they exist are very, very OOP-ish in their design, defining only virtual methods. Operators, on the other hand, were designed to be non-virtual, probably because C# is a single dispatch language, and it would be confusing to dispatch only on the runtime type of one of the operands. I'd rather see operators implemented in templates/traits/"enhanced named generic constraints" than try and shoehorn them into interfaces. |
Issue moved to dotnet/csharplang #516 via ZenHub |
I really want to see that I can use operator overloading also on Interfaces.
For example I've an interface IVector that have for example the immutable Add method :
IVector Add(IVector operand);
instead of
IVector x = a.Add(b);
I want to write
IVector x = a + b;
I don't see any reason why operator overloading is restricted to work only on normal classes.
The text was updated successfully, but these errors were encountered: