Compare types with `static if` in template function.

vladde via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 29 10:48:52 PDT 2015


I want to have a template function where I can throw in some 
stuff in the parameters, then run foreach+static ifs and check 
what type the current is. From my project clayers[1], I've taken 
out some parts. (Note the current code does not compile.)
This is what I have so far:


struct XY {size_t x, y;}
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;
         }
     }
}
[...]
changeSlot(XY(10, 15), fg.red);


For this demonstation I've chosen to only check for `char` and 
`fg`, but it's intended to check for multiple different types. 
(See [1])

Now when I try to compile, I get "Error: more than one argument 
for construction of int" for checking against fg. HOWEVER, the 
code functions properly when checking the type for characters. 
 From what I understand, my thinking works, but checking with `fg` 
is causing some crux-flux. `fg` is created from a template, 
please take a look at [2].

What am I doing wrong when I check for the type of `fg`?

TL;DR: Is there a way to check the type of `fg` with an static if?

[1] = 
https://github.com/vladdeSV/clayers/blob/change-slot/source/clayers.d#L323
[2] = 
https://github.com/yamadapc/d-colorize/blob/master/source/colorize/colors.d#L37


More information about the Digitalmars-d-learn mailing list