Proposal: Generic types should support operators #108
Replies: 16 comments
-
@scottdorman :)
A proposal for artificial I don't know what it means yet but just a thought. |
Beta Was this translation helpful? Give feedback.
-
The championed proposal #110 would provide a way to do this. |
Beta Was this translation helpful? Give feedback.
-
@eyalsk Yes, that's a better syntax than what I wrote. :) @gafter I looked for a proposal that would address this but didn't see one. If implementing #110 provides this feature, then I'm good to close this in favor of that proposal. |
Beta Was this translation helpful? Give feedback.
-
@scottdorman Yes it does solve this, you can actually create a |
Beta Was this translation helpful? Give feedback.
-
@eyalsk Thanks for that clarification. So doing that would allow us to do it but means we have to do a lot of extra work. In that case, I'm in favor of keeping this proposal since I think the syntax of using |
Beta Was this translation helpful? Give feedback.
-
@scottdorman Well I guess that there's some more design work so I'm not sure whether we will have to do a lot of extra work, also, if this feature is going to be implemented they can always introduce |
Beta Was this translation helpful? Give feedback.
-
That would only work if introducing that syntax sugar also carried with it any work that needs to be done to support generic operators. I guess until we know how much extra work might be required for us, as the consumers of the feature, and how many times we'll have to rewrite that work in each of the various projects we might need to use this feature, I'm inclined to leave this request open. |
Beta Was this translation helpful? Give feedback.
-
I think that the issue name is not 100% clear. Generic types already support operators (as long as the operators themselves don't introduce new generic parameters themselves). It's generic type parameters that should support support operators. I am not sure if #110 will support operators directly. Current proposal draft doesn't provide any syntax sugar for operators. |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Well, depends how you look at it, I mean instances will have to be specific as far as I can tell but it makes a lot of sense in a type safe language, what would the operator plus or multiplication would mean for a generic type? |
Beta Was this translation helpful? Give feedback.
-
As we work on #110 we intend to permit the compiler to infer everything possible rather than require the programmer to spell it out (e.g. that unless otherwise specified, the |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox, @eyalsk The title was copied from the initial proposal in the Roslyn repo. For me, the goal is to be able to write code like this: public static T Between(this T expression, T min, T max) where T: number
{
return expression > minimum && expression < maximum;
} Right now, I can do something similar but it requires separate methods for each of the numeric types. If I want to reduce the amount of duplicated code, I can use a private method that makes use of an |
Beta Was this translation helpful? Give feedback.
-
See https://github.com/MattWindsor91/roslyn/blob/master/concepts/docs/csconcepts.md#sugar-operator-overloading for operator overloading in the "concepts" proposal. |
Beta Was this translation helpful? Give feedback.
-
In that case, I think it would allow me to do what I showed in my earlier comment. |
Beta Was this translation helpful? Give feedback.
-
@gafter Pretty cool, thanks. :) |
Beta Was this translation helpful? Give feedback.
-
is possible already:
I'm using that in many generic functions. You can easily add that into your Between(). But I also need generic operators, or "operator" constraints to generic types, or a "numerical" constraint to generic types, well, anything that would limit T to [byte, sbyte, ushort, short, uint, int, ulong, long, float, double, BigInt, Decimal], which actually are all types that you can do calculations with. |
Beta Was this translation helpful? Give feedback.
-
@TobiasKnauss You'd be interested in #1711, then. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Copied from dotnet/roslyn#2147
C# is limited by the support for generic constraints offered by the CLR which only really supports
class
,struct
,new()
, a subclass and/or a list of interfaces.Generic types should support operators, allowing code similar to:
Beta Was this translation helpful? Give feedback.
All reactions