__traits(compiles,...) <=> ? is(typeof(...))

Jonathan M Davis jmdavisProg at gmx.com
Mon Oct 29 04:03:10 PDT 2012


On Monday, October 29, 2012 11:42:59 Zhenya wrote:
> Hi!
> 
> Tell me please,in this code first and second static if,are these
> equivalent?
> with arg = 1, __traits(compiles,"check(arg);") = true,
> is(typeof(check(arg))) = false.

In principle, is(typeof(code)) checks whether the code in there is 
syntatically and semantically valid but does _not_ check whether the code 
actually compiles. For instance, it checks for the existence of the symbols 
that you use in it, but it doesn't check whether you can actually use the 
symbol (e.g. it's private in another module).

__traits(compiles, code) on the other hand specifically checks whether the code 
will compile (so it takes stuff like access level into account). In general, 
they're identical, but depending on the symbols used in them, they can be 
subtly different.

And as Philippe points out, neither of them is intended to take strings. 
They'll pass with the string, but all they'll be checking is that it's a valid 
string, which would be pretty useless.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list