Improvements on Generics

Topics: C# Language Design
Jun 4, 2014 at 9:41 PM
I guess that my suggestion is an all-the-time repeated Topic regarding generics. We are dealing with a lot of mathematical routines which have to very fast. We did tests on a lot of situations and found that only using unsafe pointers can bring the Speed we Need. This could be done for a single type but we are dealing with many different data types, like int, double, float, etc. There I started to create a generic class. BUT:

I found out that it is impossible to put pointers on generics. I can put pointers to structs by using the where clause but this is not enough. I Need something like a numeric datatype that allows at least the Basic mathematical methods.

The same is true for casts. When I Need to cast an Array from float[] to int[] - and we are doing this a lot - the is no Chance to do this in a closed way. My method to cast from one Array type to another is 4000 lines Long without much intelligence.

Having a numeric type that allows arithmetic and casts would be very nice and could also improve your Math class in that that you can leave the types open and not binding it to double and float.
Jun 5, 2014 at 9:02 AM
You can create an abstract Calculator<T> class with Add, Substract, Multiply, etc... and the implement IntCalculator : Calculator<int>.

You'll have some performance penalty for doing the virtual call but maybe is ok.
Jun 6, 2014 at 10:12 PM
Edited Jun 6, 2014 at 10:12 PM
@msedi: please read this thread https://roslyn.codeplex.com/discussions/543883
Marked as answer by msedi on 9/10/2014 at 4:44 AM