I have the following piece of code:
public interface IVector<T>
{
static abstract int Length { get; }
ref T this[int index] { get; }
}
public static class VectorExtensions
{
public static TElement LengthSquared<TVector, TElement>(this TVector vector)
where TVector : IVector<TElement>
where TElement : INumberBase<TElement>
{
TElement lengthSquared = TElement.Zero;
for (int i = 0; i < TVector.Length; ++i)
{
lengthSquared += vector[i] * vector[i];
}
return lengthSquared;
}
public static TElement Length<TVector, TElement>(this TVector vector)
where TVector : IVector<TElement>
where TElement : INumberBase<TElement>, IRootFunctions<TElement>
{
return TElement.Sqrt(vector.LengthSquared());
}
}
Which sadly produces the following error in the second method:
The type arguments for method 'VectorExtensions.LengthSquared<TVector, TElement>(TVector)' cannot be inferred from the usage.
Is there a way to structure this code so that it can infer the type of TElement
?