Compare types with `static if` in template function.

vladde via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 29 11:04:47 PDT 2015


On Wednesday, 29 July 2015 at 17:52:45 UTC, Ali Çehreli wrote:
> On 07/29/2015 10:48 AM, vladde wrote:
>
> >          static if(is(typeof(c) == dchar) || is(typeof(c) ==
> char))
> >          {
> >              slots[xy.y][xy.x].character = c;
> >          }
> >          else if(is(typeof(c) == fg))
>
> I don't know whether it is the reason here but you fell for one 
> of the D traps. :( Most definitely, you want an 'else static 
> if' there.
>
> Otherwise, for the fg case, what ends up in your code to be 
> compiled is this:
>
>   if(is(typeof(c) == fg))
>
> Ali

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


More information about the Digitalmars-d-learn mailing list