static if and templates

Oliver oliver.ruebenkoenig at BLUBweb.de
Thu Sep 27 03:28:22 PDT 2007


Christian,

thank you for your reply and your code.

> Static if requires the condition to be evaluated at compile time, but expr
> is a run time construct. You can make these two compile-time evaluateable
> by switching to templates:

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?
 
Oliver

> 
> ---
> import std.stdio;
> 
> template isArray(T){
>     const isArray = (T.mangleof)[0] == 'A';
> }
> 
> template isMatrix(T){
>     static if ( isArray!(T) )
>         const isMatrix = (T.mangleof)[1] == 'A';
>     else
>         const isMatrix = false;
> }
> 
> void main () {
>     double s = 1.;
>     double[] v = [1.,2.];
>     double[][] m = [[1.,2.]];
> 
>     writefln("s: ", isArray!(typeof(s)), " ", isMatrix!(typeof(s)) );
>     writefln("v: ", isArray!(typeof(v)), " ", isMatrix!(typeof(v)) );
>     writefln("m: ", isArray!(typeof(m)), " ", isMatrix!(typeof(m)) );
> }
> 



More information about the Digitalmars-d-learn mailing list