On Thursday, 11 April 2019 at 20:49:45 UTC, bauss wrote:
On Thursday, 11 April 2019 at 18:13:48 UTC, Alex wrote:
The following code works when I comment out the static if
//static if (__traits(compiles, __traits(getAttributes, T)))
static foreach(a; __traits(getAttributes, T)) Attributes ~=
There seems to be absolutely no reason why this code would
fail with the static if but pass without it but in the first
case I get no attributes because the __traits compiles fails.
__traits(compiles, __traits(getAttributes, T))
vs
__traits(getAttributes, T)
How could it not compile in the first case and yet work in the
foreach?
T is a local variable name passed to this code, which does
generally return an
error when used in certain ways:
int x;
Code!x; // Basically the code at the top
if I try to do too much with x I get `cannot use local `x` as
a parameter to a non-global template`. Usually when I try to
pass T to another template I get this error.
But some things work. But I can't for the life of me
understand why compiles is failing but the foreach loop is
working.
The problem is that I am using compiles quite often and either
I'm misusing it or it's got some bugs in it that worry me
because maybe they are silently failing?
Note that I get no errors in my code and I do get the
attributes so it is technically working.
It would be really nice to know why the compiles is failing,
e.g.,
pragma(compilesmsg, __traits(getAttributes, T)) spits out the
error.
Must be something else in your code because it works fine for
me with this shortened example.
https://run.dlang.io/is/o8xESB
Seriously? Do you think you have ESP? Your code isn't even close
to was talking about ;/
Here is is updated that shows the error. You seem to fail to
understand that it is impossible for it to be my code. There is
no way the static if should fail yet the static foreach pass.
They both go together, that is is the whole point of the static
if, to make sure the foreach works, yet it says it doesn't work
when it in fact does(as commenting out the line proves).
import std.stdio, std.traits, std.conv;
struct A(alias T)
{
static void foo()
{
static if (__traits(compiles, __traits(getAttributes, T))) //
Failing for some reason
static foreach(a; __traits(getAttributes, T)) pragma(msg,
to!string(a), "-", typeof(a).stringof);
}
}
void main()
{
@(3) int a;
(A!a).foo();
}