[Proposal][Operator Overloading Enhancement] #2065
Replies: 23 comments
-
There are a number of issues with this proposal: 1: Operators are static methods, and so cannot by definition be virtual. Making them instance methods would break them if they were ever called on a null instance. This would be a huge issue!
|
Beta Was this translation helpful? Give feedback.
-
My main issue with this proposal is that I don't see how doing this would be worth it. You're proposing adding a feature that will make writing one very specific, very rare pattern easier. I don't think making the language more complicate for that makes sense. |
Beta Was this translation helpful? Give feedback.
-
It will make easier to extend the existing class without worrying that somebody will cast class to parent and will use wrong operator |
Beta Was this translation helpful? Give feedback.
-
In addition to the other issues already mentioned, this would break existing code. |
Beta Was this translation helpful? Give feedback.
-
Why ? Just throw NullPointerException as for other instance methods. It is huge issue that somebody can cast my class to parent and ignore proper operator for instance
No !! I suggest old solution and new virtual instance operator methods to live together. |
Beta Was this translation helpful? Give feedback.
-
No it will not. I suggest to coexist old style and new style together (static operators and virtual instance operators) |
Beta Was this translation helpful? Give feedback.
-
Why is that a problem? This is how all static methods work. They're statically dispatched. |
Beta Was this translation helpful? Give feedback.
-
In general operator overloading is only recommended when it's abundantly clear what the operators do. In practice this means outside mathsy code, operator overloading is probably best avoided. If the code won't work unless it's virtually dispatched, then it probably shouldn't be an operator to start off with. |
Beta Was this translation helpful? Give feedback.
-
it's also super weird that you'd have an operator that behaved differently on a subclass. Sounds like a recipe for tons of confusion. |
Beta Was this translation helpful? Give feedback.
-
I must've misread your proposal but where did you mention that? Even if you do have the "two style" option somewhere, that is still bad and complicates code. Imagine one person marks the class in the new style, then another dev starts working on it, not realizing that things are implicitly virtual (bad idea) and implicitly overriden (even worse idea). This would be completely inconsistent with the language, as well as requiring to rewrite how operators are written as they are static methods. If you really want that behavior, then you make a method, it's that simple. That's what people've been doing for years now. Clearly the result you want is too complicated to be an operator. Also, |
Beta Was this translation helpful? Give feedback.
-
Guys, I think all @redradist is asking for is instance operators. Well, I say all--it is a huge change, but not a crazy one. Why is it not possible to have Not saying I put my weight behind this proposal, just saying it's more reasonable than you're making out. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Thank you @Richiban for support !! |
Beta Was this translation helpful? Give feedback.
-
I have read this topic some time ago and also because of this article I have decided to write this proposal. According this article the main reason not to implement an instance operators was the following: |
Beta Was this translation helpful? Give feedback.
-
Yes, it could be solved, but that just adds more complexity. The designers of the language intentionally wanted to avoid those kinds of complexity and to keep operators simple. Operators don't exist to replace methods in the language and the language designers very intentionally wanted to avoid the character salad that C++ partially turned into. As that blog post mentions it's fairly easy to recreate a virtual dispatch by having the static operators invoke instance methods. Given how easy it is to accomplish, and how rare such operators should be, it doesn't make much sense to encode it directly in the language. That would only encourage its use, and I think that you'd need to demonstrate good use cases where such operators are worth it. Your use cases are, if anything, examples of where you shouldn't be using operators. |
Beta Was this translation helpful? Give feedback.
-
Example, no problems. I do not tell that adding contextual keywords is a good idea ... |
Beta Was this translation helpful? Give feedback.
-
This is not a good example . A graphics engine would not use classes here. They would use structs. So inheritance isn't interesting in this case. However, for the sake of argument, say you did these as classes, and you used inheritance like so So, even with your example, i'm not seeing any need for virtual ops here (and that was being generous since you wouldn't actually do things this way in a real engine). |
Beta Was this translation helpful? Give feedback.
-
Such types already exist. Even if they didn't, you'd want to implement them as structs, not classes, especially in a graphics engine. There's no need for such types to be polymorphic or necessitate virtual dispatch. And lastly, even if you absolutely wanted to do this, you can. Write a static operator and have it call a virtual method. Yes, you're slightly inconvenienced, but how many times can that possibly come up? Certainly not enough to justify a language change, especially one that reverses a very intentional design choice. |
Beta Was this translation helpful? Give feedback.
-
Note: @HaloFour are two different people. He's not an alt-account of mine. I swear! |
Beta Was this translation helpful? Give feedback.
-
I've always secretly wanted to be a llama. I wonder what llama bacon tastes like? |
Beta Was this translation helpful? Give feedback.
-
@HaloFour wouldn't that be cannibalism?
…On Wed, 12 Dec 2018, 20:09 HaloFour, ***@***.***> wrote:
I've always secreted wanted to be a llama. I wonder what llama bacon
tastes like?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/dotnet/csharplang/issues/2065#issuecomment-446726615>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAoyj3pzizUw2KKw2QDblmbisADQKUR1ks5u4WJ2gaJpZM4ZOUUo>
.
|
Beta Was this translation helpful? Give feedback.
-
There's really nothing gained from it. There's more downsides than upsides to this, such as complexity and people abusing and misusing operators even more. |
Beta Was this translation helpful? Give feedback.
-
Related: dotnet/roslyn#5909 (tldr: wontfix) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Issue
Consider the following code:
This seems inconsistent and not obvious.
Solution to it is obvious:
But it is error-prone to make helper virtual function every time. Compiler can generate it instead
Proposal
Consider adding virtual operator member function like in the following example:
For asymmetric operator behavior you should write something like this:
Under the hood it will work as follows as this first example by adding the helper function under hood
Beta Was this translation helpful? Give feedback.
All reactions