Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure the template type is scalar in D?

I have two functions: one is scalar multiplication for a vector and other - vector-matrix multiplication:

pure T[] mul(S, T)(S s, T[] a)

and

pure T[] mul(T)(T[] a, T[][] B)

Of course this leads to a conflict, as S can be a vector too, so that first template covers the second. How do I tell compiler, I want only scalar type to be S?

like image 865
akalenuk Avatar asked Oct 06 '13 16:10

akalenuk


1 Answers

You need to use a template constraint.

pure T[] mul(S, T)(S s, T[] a) if (isScalarType!S)

This declares that the template should only be considered when isScalarType!S is true.

isScalarType can be found in std.traits.

In D, the scalar types are the numeric types, character types, and bool. You can restrict further using the other traits from std.traits if you like (e.g. isNumeric or isFloatingPoint).

like image 153
Peter Alexander Avatar answered Oct 30 '22 06:10

Peter Alexander