operator overloading: where is this going? #1606
-
I raised this years ago and had to revisit it in my current mathematical c# project. As a mathematical programming type, I have had to bite my self--knowing I can't do things that What is the status on this enhancement, especially since are you no longer driving or rideing https://stackoverflow.com/questions/1687395/overloading-the-operator-to-add-two-arrays What's wrong with this C# code? I tried to overload the + operator to add two arrays, but got an error message as follows: One of the parameters of a binary operator must be the containing type. class Program
} public static int[] operator+(int[] x, int[] y)
} We are considering adding "extension operators" to a hypothetical future version of C#, which would solve your problem. Straw poll: do any of you have AWESOME scenarios for this feature? The more awesome REALISTIC scenarios we can get, the more likely a feature is to be implemented some day. Send 'em my way; you can use the email link on my blog. – Eric Lippert Nov 6 '09 at 15:15 Thanks Eric. Does it mean we will also get "Extension Everything" :) – Joan Venge Nov 6 '09 at 21:26 How do we do this or should I abandon polymorphism until I am polytheistic? – dr d b karron Jun 13 '16 at 5:17 |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
Extension operators would be part of "extension everything" feature, which is still in an early phase. See dotnet/roslyn#11159 and #192. |
Beta Was this translation helpful? Give feedback.
-
@drdbkarron You should wrap your array in a custom type. It could be a struct or a class. Define the operators in that new type, and have it return an instance of the new type. That is currently the best way to accomplish what you're trying to do. Our possible "extension everything" feature may end up being tied together with "type classes" in its design, and therefore we are unlikely to do one without doing the other (for fear we might design ourselves into a corner). C# 8 is possible, but I would guess that C# 9 is more likely. |
Beta Was this translation helpful? Give feedback.
-
Related to your example of element-wise addition (different operator): #431. Also #823 suggests the |
Beta Was this translation helpful? Give feedback.
-
I fully agree that we need some "global" mechanism to add additional operators, methods, etc. to existing classes. I was first thinking that something that C++ could do would be the right solution, but I must admit that the "extension everything" approach is the better way. The only problem I see that for operator overloading, we urgently need the feature also apply mathematical operations on generics. We wrote a large library with a lot of overloads to manage this as this is also the case for System.Math. |
Beta Was this translation helpful? Give feedback.
-
"Mathematical operations on generics" sure would be a really huge thing! |
Beta Was this translation helpful? Give feedback.
-
Add me in on that 2! |
Beta Was this translation helpful? Give feedback.
-
The feature request generally aligns with our needs to, but is very vague. |
Beta Was this translation helpful? Give feedback.
-
@lostmsu why is it vague? public static T operator *<T>(T[] a, T[] b) where T : IMultipliable
{
....
} |
Beta Was this translation helpful? Give feedback.
Extension operators would be part of "extension everything" feature, which is still in an early phase. See dotnet/roslyn#11159 and #192.