Can you explain this?

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 20 14:06:47 PDT 2014


On Wednesday, 20 August 2014 at 20:39:42 UTC, Jonathan M Davis 
wrote:
> is(typeof(foo)) and __traits(compiles, foo) are not the same. 
> The first tests for the existence of the symbol, whereas the 
> second checks whether the code will actually compile.

Is that even true? I mean, are you just repeating something 
you've heard, or have you checked? "is(typeof(foo))" has always 
failed for me merelly if "foo" fails to compile. "foo" being an 
existing (but private) symbol is enough.

Test case:
//----
module foo;

struct S
{
     private int i;
}
//----
import foo;

void main(string[] args)
{
     S s;
     writeln(is(typeof(s.i)));
     writeln(__traits(compiles, s.i));
}
//----

This says false, false.

I've yet to find a usecase where "is(typeof(...))" and 
"__traits(compiles, ...)" aren't interchangeable.

I mean, I may have missed something, but it seems the whole 
"private" thing is just miss-information.


More information about the Digitalmars-d-learn mailing list