You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Java generic type constraints translated incorrectly #16
Note that in this case there is no C# equivalent of constraining a generic type parameter to be a number, but at least we can make it syntactically correct.
Mads Torgerson an his team are currently working on allowing static abstract members in interfaces. The motivation is to allow operators in interfaces. So, a future version of C# might well have a INumeric<T> interface defined on numeric types.
After fixing #26, I'm now turning my attention to type constraints. I'm thinking that TypeHelper.ConvertType could return an object instead of string, that would include a list of type parameters with their constraints/bounds. A generic type or method definition could then add these as type constraints with the where clause. Might be a little ambitious, but I'll see what I can do :-)
I tried to use INumber as a generic constraint, but it's a INumber<TSelf> interface in fact, so it defeats the purpose.
There is no non-generic version of the INumber interface.
You could use where T : unmanaged which would only accept pure value types (enums, pointers, numbers, and user defined value types which do not contain reference types), but that's not a strict equivalent.
The proper translation would be to implement non-generic overloads for each number type (int, nint, Half, Double, Float, sbyte, short, long, and their unsigned variants)... instead of a generic method.
The proper translation would be to implement non-generic overloads for each number type (int, nint, Half, Double, Float, sbyte, short, long, and their unsigned variants)... instead of a generic method.
Yeah... we're not going to do that right now 😄
I just confirmed that INumber<T> should work in this case, the only problem is that the 0 literal isn't supported for that comparison, you have to use T.Zero:
I'd be curious to see how you can make it work with INumber. Since it's self referencing (for example in the byte class it implements INumber<byte> ) , I was under the impression after some tests that a generic method for all numeric types wasn't possible.
To make it work, the self-referencing type parameter (CRTP-style) needs to reference T, not the specific types.
So in my example above (which I confirmed works in .NET 7), IsNegative<T>(T number) where T : INumber<T> becomes reified as IsNegative(byte number) since byte implements INumber<byte>.
Activity
OJacot-Descombes commentedon Jun 30, 2021
Mads Torgerson an his team are currently working on allowing static abstract members in interfaces. The motivation is to allow operators in interfaces. So, a future version of C# might well have a
INumeric<T>interface defined on numeric types.paulirwin commentedon Jul 4, 2021
After fixing #26, I'm now turning my attention to type constraints. I'm thinking that
TypeHelper.ConvertTypecould return an object instead ofstring, that would include a list of type parameters with their constraints/bounds. A generic type or method definition could then add these as type constraints with thewhereclause. Might be a little ambitious, but I'll see what I can do :-)OJacot-Descombes commentedon Apr 2, 2023
C# 11 now features Static abstract members in interfaces and the INumber Interface has been added to .NET 7.
maximilien-noal commentedon Sep 5, 2023
I tried to use INumber as a generic constraint, but it's a
INumber<TSelf>interface in fact, so it defeats the purpose.There is no non-generic version of the INumber interface.
You could use
where T : unmanagedwhich would only accept pure value types (enums, pointers, numbers, and user defined value types which do not contain reference types), but that's not a strict equivalent.maximilien-noal commentedon Sep 8, 2023
The proper translation would be to implement non-generic overloads for each number type (int, nint, Half, Double, Float, sbyte, short, long, and their unsigned variants)... instead of a generic method.
paulirwin commentedon Sep 8, 2023
Yeah... we're not going to do that right now 😄
I just confirmed that
INumber<T>should work in this case, the only problem is that the 0 literal isn't supported for that comparison, you have to useT.Zero:I don't know if we will get to that level of translation for the 0 literal, but at least theoretically it is possible to use
INumber<T>here.maximilien-noal commentedon Sep 8, 2023
I'd be curious to see how you can make it work with INumber. Since it's self referencing (for example in the byte class it implements
INumber<byte>) , I was under the impression after some tests that a generic method for all numeric types wasn't possible.paulirwin commentedon Sep 8, 2023
To make it work, the self-referencing type parameter (CRTP-style) needs to reference
T, not the specific types.So in my example above (which I confirmed works in .NET 7),
IsNegative<T>(T number) where T : INumber<T>becomes reified asIsNegative(byte number)sincebyteimplementsINumber<byte>.The full method then gets reified as if it were:
That pseudocode directly above doesn't exactly work as is, since the
Zeroproperty is explicitly implemented, but it does work as a generic type parameter.(Edit: also, of course, this method would always return false 😄 )
maximilien-noal commentedon Sep 8, 2023
It was so simple ! This will help a lot. Thank you !