Generic with type constraint???

O

O.o

Here's something that's been puzzling me:

I have been developing a library for digital signal processing in C#.
I find I have a _lot_ of repeated code, because all of the methods are
**absolutely identical**, apart from the type of one or more input
argument(s), which may be int, uint, float, double, decimal, or
complex numbers composed of any of those types. It is not only tedious
to code, but it is also a bit of a nightmare to manage because I have
to make the same changes in 10 different places. Using a generic type
seemed like the way out, but there is no way (I can find) of
restricting <T> to a numeric type, so you can't use such things as
basic mathematical operators, among other problems (e.g. complex is a
nullable class but integral types are not). Using the "object" type
leads to a similar dead end.

My question then is this: why isn't there a "numeric" class from which
the numeric types inherit, or at the very least an interface that can
only be implemented by numeric types?

O.o
 
B

Ben Voigt [C++ MVP]

O.o said:
Here's something that's been puzzling me:

I have been developing a library for digital signal processing in C#.
I find I have a _lot_ of repeated code, because all of the methods are
**absolutely identical**, apart from the type of one or more input
argument(s), which may be int, uint, float, double, decimal, or
complex numbers composed of any of those types. It is not only tedious
to code, but it is also a bit of a nightmare to manage because I have
to make the same changes in 10 different places. Using a generic type
seemed like the way out, but there is no way (I can find) of
restricting <T> to a numeric type, so you can't use such things as
basic mathematical operators, among other problems (e.g. complex is a
nullable class but integral types are not). Using the "object" type
leads to a similar dead end.

My question then is this: why isn't there a "numeric" class from which
the numeric types inherit, or at the very least an interface that can
only be implemented by numeric types?
http://www.yoda.arachsys.com/csharp/miscutil/usage/genericoperators.html


O.o
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top