Compare types with `static if` in template function.

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 29 11:14:10 PDT 2015


On 07/29/2015 11:08 AM, vladde wrote:
> On Wednesday, 29 July 2015 at 18:04:48 UTC, vladde wrote:
>> Apparently, if I only check for a character the code compiles without
>> the need of static if.
>>
>> if(is(typeof(c) == dchar) || is(typeof(c) == char)){
>> slots[xy.y][xy.x].character = c; } //Compiles and works as expected
>
> And changing all if statements to static if still does not compile.

The error may be the capital C in the else branch. (?)

Can you provide complete code please? The following compiles with dmd 2.067:

struct XY {size_t x, y;}

private template color_type(int offset)
{
   static enum type : int
   {
     init = 39 + offset,

     black   = 30 + offset,
     red     = 31 + offset,
     green   = 32 + offset,
     yellow  = 33 + offset,
     blue    = 34 + offset,
     magenta = 35 + offset,
     cyan    = 36 + offset,
     white   = 37 + offset,

     light_black   = 90 + offset,
     light_red     = 91 + offset,
     light_green   = 92 + offset,
     light_yellow  = 93 + offset,
     light_blue    = 94 + offset,
     light_magenta = 95 + offset,
     light_cyan    = 96 + offset,
     light_white   = 97 + offset
   }
}

alias color_type!0 .type fg;

// alias fg = colorize.fg; //[2]

void changeSlot(C...)(XY xy, C changes){
     foreach(c; changes){
         static if(is(typeof(c) == dchar) || is(typeof(c) == char))
         {
             // slots[xy.y][xy.x].character = c;
         }
         else if(is(typeof(c) == fg))
         {
             // slots[xy.y][xy.x].fg = C;
         }
     }
}

void main()
{
     changeSlot(XY(10, 15), fg.red);
}

Ali



More information about the Digitalmars-d-learn mailing list