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

Jonathan M Davis jmdavisProg at gmx.com
Mon Oct 29 16:17:22 PDT 2012


On Monday, October 29, 2012 23:38:34 Timon Gehr wrote:
> On 10/29/2012 12:03 PM, Jonathan M Davis wrote:
> > 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).
> > ...
> 
> Accessing private symbols is always illegal, even within typeof expressions.

As I understand it, that's not supposed to be the case. is(typeof(T.init)) 
tests for existance not for compilability, and private symbols are fully 
visible by everything that imports the module that they're in. They're just 
not accessible. I completely agree that it would be better for them to be 
hidden as well, but it doesn't work that way right now, and no one has been 
able to convince Walter that it should.

Another example would be the init property of a type where you can't use its 
init property (as occurs with non-static Voldemort types). Assuming that it 
works properly, is(typeof(T.init)) will be true, but __traits(compiles, 
T.init) won't.

There's a discussion on it in this bug report:

http://d.puremagic.com/issues/show_bug.cgi?id=8339

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list