Template error on compiling ...

Meta jared771 at gmail.com
Fri Feb 28 21:49:49 PST 2014


EOn Saturday, 1 March 2014 at 00:17:55 UTC, Stanislav Blinov 
wrote:
> /// Tests if type M is a Matrix
> enum bool isSomeMatrix(M) = is(M == Matrix!T, T);

Unrelated, but it may be of some interest. This isn't really a
good way to define your isSomeMatrix template, i.e., tying it to
a specific type. What if you want to extend your matrix library
in the future to include ScalarMatrix, UpperTriangularMatrix,
etc. implementations? While these are matrices, they will fail
isSomeMatrix as it is defined. A better way when using templates
is to define a number of primitives required for an object to be
a matrix, and then test for those primitives in isSomeMatrix to
determine if M is a matrix. This is the concept of duck-typing,
and its how D's ranges are defined. This way, anything that
implements the range protocol can be operated on by any range
algorithm. It's quite flexible and powerful.


More information about the Digitalmars-d-learn mailing list