static if and templates
Christian Kamm
kamm.incasoftware at shift-at-left-and-remove-this.de
Thu Sep 27 04:05:32 PDT 2007
> what i do not understand is the following: if expr is a runtime construct,
> how come expr.mangleof works? for this to work (in my beginner thinking)
> the compiler takes, say m, from main and inserts it into expr.mangleof.
> Now, why is it not possible for the compile to insert the expr into
> isArray? I think there is some fundamental thing i don't understand. Any
> wisdom you can share?
Well, I'm pretty sure expr.mangleof becomes typeof(expr).mangleof, so
---
class A {}
class B : A {}
A a = new B;
writefln(a.mangleof);
---
Would give you the mangled name of A (compile time type) and not of B.
By the way, a more conventional way of writing your isArray and isMatrix
functions would be
---
template isArray(T){
static if(is(T U : U[]))
const isArray = true;
else
const isArray = false;
}
template isMatrix(T){
static if(is(T U : U[][]))
const isMatrix = true;
else
const isMatrix = false;
}
---
And finally, I don't recommend using T[][] as a matrix type: it's literally
an array of arrays, so each row mat[i] does not even need to have the same
length and you won't get very good performance.
So, unless that's what you want, I recommend looking through the projects at
dsource for implementations of vectors and matrices - I think there are
several.
Cheers,
Christian
More information about the Digitalmars-d-learn
mailing list