March 01, 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.
March 01, 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.
March 01, 2014
Hiho,

thank you for your responses.
This D language meta programming sounds funny and strange to
someone who has never really worked with something like this but
I understand its importance and will look more into working with
it. =)

Robin
1 2
Next ›   Last »