Skip to content
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

Closed
stefc opened this issue Oct 1, 2015 · 10 comments
Closed

[Proposal] Operator overloading on Interfaces #5624

stefc opened this issue Oct 1, 2015 · 10 comments

Comments

@stefc
Copy link

stefc commented Oct 1, 2015

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.

@AlgorithmsAreCool
Copy link

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?

@stefc
Copy link
Author

stefc commented Oct 1, 2015

I think operator overloading can be defined the same as extension methods, somewhere in a static class besides the interface declaration.

@AlgorithmsAreCool
Copy link

Would this change also allow operator definitions for all types from extension methods?

@AlgorithmsAreCool
Copy link

See also related discussion

Static interface members
https://roslyn.codeplex.com/discussions/541436

Possibility to provide operators in interface
https://roslyn.codeplex.com/discussions/570704

Static Interfaces Proposal
#2204

@daveaglick
Copy link
Contributor

FYI, there's also related discussion (particularly of static extensions and static interfaces) at #996 and #2204. #112 also looks related, though it's about extension properties and not operators.

@stefc
Copy link
Author

stefc commented Oct 1, 2015

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.
For the sideeffect with an existing operator overloading exsist on the class the compiler should be inform with a warning on that and the developer should be aware of this amigous usage. In my case I use either the interface operators or the class operator, if would define both it would be bad design I think. I see the interface as abstraction of the internals of a class.

@orthoxerox
Copy link
Contributor

@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.

@ashmind
Copy link
Contributor

ashmind commented Oct 1, 2015

I think this should be done through extension operators -- so it should be covered by #5165 / #4945.

@aluanhaddad
Copy link

@ashmind agreed. #5165 is definitely the better proposal.

@gafter
Copy link
Member

gafter commented Apr 28, 2017

Issue moved to dotnet/csharplang #516 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants