www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - is(T t == U!n, U, int n)

reply =?UTF-8?B?THXDrXM=?= Marques <luis luismarques.eu> writes:
If this works:

     template X(T)
     {
         static if(is(T t == S!n, int n))
         {
             static if(n == 0)
                 alias X = AliasSeq!();
             else
                 alias X = S!0;
         }
         else
         {
             static assert(0);
         }
     }

     class S(int n) { }

     pragma(msg, X!(S!3)); // S!0
     pragma(msg, X!(S!0)); // ()

...why can't I generalize it to match U!n, for some U, int n?

     template X(T)
     {
         static if(is(T t == U!n, U, int n))
         {
             static if(n == 0)
                 alias X = AliasSeq!();
             else
                 alias X = U!0;
         }
         else
         {
             static assert(0);
         }
     }

     class S(int n) { }

     pragma(msg, X!(S!3)); // static assert: 0
Jun 13 2018
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Wednesday, 13 June 2018 at 09:41:45 UTC, Luís Marques wrote:

 ...why can't I generalize it to match U!n, for some U, int n?

     template X(T)
     {
         static if(is(T t == U!n, U, int n))
U needs to be alias U, since S is not a type, but a template. This works: template X(T) { static if(is(T t == U!n, alias U, int n)) { static if(n == 0) alias X = AliasSeq!(); else alias X = U!0; } else { static assert(0); } } class S(int n) { } pragma(msg, X!(S!3)); Also, you might want to consider the Learn forum for these kinds of questions in the future. :) -- Simen
Jun 13 2018
parent =?UTF-8?B?THXDrXM=?= Marques <luis luismarques.eu> writes:
On Wednesday, 13 June 2018 at 10:34:48 UTC, Simen Kjærås wrote:
 Also, you might want to consider the Learn forum for these 
 kinds of questions in the future. :)
You're right. What happened is that I went to check the syntax in the "D Templates: A Tutorial" PDF, and it has some material on how some possible uses of is expressions used to not be accepted by the frontend, so I though this might be a similar case: perhaps a bug or an unsupported scenario. A bit arrogant on my part, but I do stumble into bugs from time to time, so it was plausible :-). And if it was a bug/limitation then arguably this was the better forum.
Jun 13 2018