Template error on compiling ...

Stanislav Blinov stanislav.blinov at gmail.com
Fri Feb 28 16:17:53 PST 2014


On Friday, 28 February 2014 at 23:16:48 UTC, Robin wrote:
> Hiho,
>
> just for you: the whole code! =)
> http://dpaste.dzfl.pl/cd1537571a4d
>
> The idea is that I have matrix instances and so-called 
> ElementalOperations which are able to operate on matrices to 
> solve certain algorithms object oriented without nasty loops 
> and so on.

I'm going to repeat the advice from your thread about matrices: 
don't use upper case in module names. See 
http://dlang.org/module.html#ModuleDeclaration.

"delete" keyword will eventually go away, you're better off not 
using it.

Now to a suggestion: how about going up a type so you don't have 
to carry around the (T = double) and Matrix!T code dependencies? 
The tests below are simplified and clumped together, but should 
be enough for an illustration.

struct Matrix(T = double) { /* blah */ }

/// Tests if type M is a Matrix
enum bool isSomeMatrix(M) = is(M == Matrix!T, T);

/// Gets element type from a Matrix type
alias MatrixElement(M : Matrix!T, T) = T;

abstract class ElementalOperation(M) if (isSomeMatrix!M) { /* 
blah */ }

unittest {
     import std.typetuple;

     alias types = TypeTuple!(byte,short,int,long,float,double);
     foreach(T; types) {
         // don't accept scalars
         static assert(!isSomeMatrix!T);
         static assert(!is(MatrixElement!T));
         static assert(!is(ElementalOperation!T));
         // accept matrices
         static assert(isSomeMatrix!(Matrix!T));
         static assert(is(MatrixElement!(Matrix!T) == T));
         static assert(is(ElementalOperation!(Matrix!T)));
     }
}

This way all ElementalOperation's method signatures will 
transform from void foo(Matrix!T m) into void foo(M m), and the 
MatrixElement template will help in cases when you do need to get 
to underlying type.

> This had worked perfectly in my java implementation so far.

Well, D is not Java ;) As Ali mentioned, Matrix is not a type, 
Matrix!T is.


More information about the Digitalmars-d-learn mailing list