Closed
Description
In my opinion, all members in the generic mathematics interfaces should be nullable reference with a ?
appended to the end of the type. For example, the current interface:
public interface IEqualityOperators<TSelf, TOther> : IEquatable<TOther> where TSelf : IEqualityOperators<TSelf, TOther>
{
static abstract bool operator ==(TSelf left, TOther right);
static abstract bool operator !=(TSelf left, TOther right);
}
Should instead be something like:
public interface IEqualityOperators<TSelf, TOther> : IEquatable<TOther> where TSelf : IEqualityOperators<TSelf, TOther>
{
static abstract bool operator ==(TSelf? left, TOther? right);
static abstract bool operator !=(TSelf? left, TOther? right);
}
Why? It causes CS8604
in various scenarios like the one below:
public class Matrix<T>
where T : INumber<T>
{
...
public static Matrix<T> FactoryIdentity(int rows, int columns)
{
if (sourceof(rows < 1, out string c1)) throw new ArgumentOutOfRangeException(nameof(rows), rows, c1);
if (sourceof(columns < 1, out string c2)) throw new ArgumentOutOfRangeException(nameof(columns), columns, c2);
Matrix<T> matrix = new(rows, columns);
if (default(T) is not null && T.Zero == default) // CS8604
{
int minimum = Math.Min(rows, columns);
for (int i = 0; i < minimum; i++)
{
matrix._matrix[i * columns + i] = T.One;
}
}
else
{
for (int row = 0, i = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
matrix._matrix[i++] = row == column ? T.One : T.Zero;
}
}
}
return matrix;
}
Note: "Matrix" is a custom type that represents a standard matrix as a flattened array.
In my opinion this should go for all the generic mathematics interfaces, not just the IEqualityOperators
members.