[Issue 19303] hasMember fails to recognize member (interaction with mixin template)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 21 11:11:49 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=19303

Boris Carvajal <boris2.9 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |boris2.9 at gmail.com

--- Comment #3 from Boris Carvajal <boris2.9 at gmail.com> ---
There are 2 issues here:

1) Currently, DMD can't see forward references that are declared inside
subsequent declaration scopes (things like 'static if' or 'static foreach'),
this is hard to solve because the compiler could jump to a later scope looking
for what it needs but that scope could also need something from the previous, a
chicken and egg problem.

2) Template instances are cached, if the first instance of 'hasMember!(S,
ElementType")' evaluates to false, it will always return false.

Code:
---
enum hasM(T, string name) = __traits(hasMember, T, name);

struct S
{
    // ElementType is not yet defined so it evaluates to false and the
    // pragma(hasM) below will print false.
    // Commenting out this 'static if' will make the pragma(hasM) print true
    // because the three pragmas are evaluated much later than 'struct S'.
    static if(hasM!(S, "ElementType")) { }

    static if(1)
        alias ElementType = int;
}

void main()
{
    pragma(msg, S.ElementType); // int
    pragma(msg, hasM!(S, "ElementType")); // depends on the first instantiation
    pragma(msg, __traits(hasMember, S, "ElementType"));  // true
}
---

--


More information about the Digitalmars-d-bugs mailing list