Can't use is expression on nested type

monkyyy crazymonkyyy at gmail.com
Fri Mar 14 01:02:02 UTC 2025


On Friday, 14 March 2025 at 00:14:35 UTC, Emma wrote:
> Hello, I was doing some template nonsense and came across this 
> weird issue. This compiles fine:
>
> ```d
> struct Foo(string S) {}
>
> bool foo(T)(T it) {
>     static if (is(T == Foo!S, string S)) {
>         return true;
>     } else {
>         return false;
>     }
> }
>
> void main() {
>     foo(Foo!"x"());
> }
> ```
>
> but the below fails with a “undefined identifier 'S'” error!
>
> ```d
> struct X {
>     struct Foo(string S) {}
> }
>
> bool foo(T)(T it) {
>     static if (is(T == X.Foo!S, string S)) {
>         return true;
>     } else {
>         return false;
>     }
> }
>
> void main() {
>     foo(X.Foo!"x"());
> }
> ```
>
> Am I just doing something obviously wrong, or is this a 
> compiler bug? If so, is there a workaround? Thank you.

is sux

```d
struct X {
     struct Foo(string S){}
     struct Foo(int i){}
}

bool foo(T)(T it) {
     static if (is(T:Temp!Args,alias Temp,Args...)) {
         //return true;
         return is(typeof(Args[0])==string);
     } else {
         return false;
     }
}

void main() {
     import std;
     foo(X().Foo!"x"()).writeln;
     foo(X().Foo!1()).writeln;
}
```


More information about the Digitalmars-d-learn mailing list