Thread overview
Can't use is expression on nested type
Mar 14
Emma
Mar 14
monkyyy
March 14

Hello, I was doing some template nonsense and came across this weird issue. This compiles fine:

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!

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.

March 14

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:

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!

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

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;
}
March 13
On 3/13/25 5:14 PM, Emma wrote:

> but the below fails with a “undefined identifier 'S'” error!
[...]
> bool foo(T)(T it) {
>      static if (is(T == X.Foo!S, string S)) {

Here is another solution that uses std.traits:

import std.traits;

struct X {
    struct Foo(string S) {}
}

bool foo(T)(T it) {
    static if (isInstanceOf!(T, X.Foo)) {
        alias S = templateArgsOf!T[0];

        // The following passes:
        // static assert(is (S == string));

        return true;

    } else {
        return false;
    }
}

void main() {
    foo(X.Foo!"x"());
}

Ali