Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic constraint for bitwise operators

So I have a class which looks something like this:

public class Foo<TKey>
{
   // ...
}

I have a method which uses the generic argument TKey as follows:

public int Test(TKey val)
{
   return val | 5;
}

I need to set constraints which ensure that TKey is a numeric value in order to use bitwise operators. Anyhow, you can not set constraints to ensure that it is a numeric value since short, int, double, ... do not implement an interface like INumeric.

Now the question is, would this be possible with only constraints?

like image 882
Twenty Avatar asked Sep 13 '25 10:09

Twenty


2 Answers

In .NET 7 interfaces for generic maths were introduced that are implemented by numeric types. There is also a special interface for bitwise operations IBitwiseOperators<TSelf,TOther,TResult> that you can use. Of course, you can adapt the TOther and TResult arguments, depending on your requirements here. For simplicity, all arguments are of type TKey in this example (see the note below).

public TKey Test<TKey>(TKey val) where TKey : IBitwiseOperators<TKey, TKey, TKey>
{
   return val | 5;
}

The generalized inteface is INumber<T> that represents any numeric type. There are also more specialized numeric categories for binary or floating point numbers, see Numeric interfaces.

Note: The way you defined your method to perform a bitwise operation with any incoming numeric type instance and return an int will not work in general. E.g. if you pass a long and OR it with 5, it is still a long, which cannot be assigned directly to an int, which is smaller.

like image 164
thatguy Avatar answered Sep 14 '25 23:09

thatguy


Generics are about allowing any Random class that any Programmer on the planet might throw in for T. However the numeric types are actually a very static list. I would never expect a programmer to make his own numeric type. Stuff with a overloaded Operators including binary ones? Maybe rarely.

So this is very much not a generic case. If you only write code for 2 - maybe 3 - types you should cover just about every generic in existence:

  • the highest range integer you have to expect signed Int64 IIRC
  • the highest range floating point you have to expect. IIRC, Decimal*.
  • optionally BigInteger, for when you have to expect really big numbers. However a short look revealed that none of Math class functions support BigInt values. They keep it to Decimal, Double and many smaler built in Numerics. So this case might have been dropped as to rare and to easy to get wrong.

*Correction: While Decimal has the highest amount of digits of precision and bigest size at 64 bit, Double has the bigger range. By an order of Magnitude, that itself has an order of Magnitude.

like image 25
Christopher Avatar answered Sep 14 '25 23:09

Christopher