Can you explain this?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 20 13:39:41 PDT 2014


On Wednesday, 20 August 2014 at 20:12:58 UTC, Justin Whear wrote:
> On Wed, 20 Aug 2014 20:01:03 +0000, Colin wrote:
>
>> It looks very....hacky.
>> 
>> I see 3 distinct parts playing a role in my confusion:
>> A) The 'is' keyword. What does it do when you have 
>> is(expression);
>> B) typeof( expression ); whats this doing? Particularly when 
>> the
>> expression its acting on is a closure that returns nothing? 
>> (at least as
>> far as I can see)
>> C) The closure expression:
>> (inout int = 0) {
>>     // Check to see if I can do InputRangy stuff...
>> }
>> Why is there a need for that inout int = 0 clause at the start 
>> of it?
>> 
>> Sorry for the long question!
>> 
>> Thanks,
>> Colin
>
> Before the introduction of __traits(compiles, ...), 
> `is(typeof(...))` was used.

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. In most cases, 
there's no real difference, but if you're trying to use a symbol 
in a context where it's not legal (e.g. using a private variable 
that you don't have access to), then the is expression will pass, 
whereas the _traits(compiles.. will fail. At least in Phobos, 
is(typeof... is used far more freqently than __traits(compiles... 
The trait is used almost exclusively in unit tests, not in 
template constraints or in user-defined traits such as 
isInputRange.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list