www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Choosing between __traits(compiles, { ... } ) and is(typeof( { ... }

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
When is

     __traits(compiles, { ... } )

preferred over

     is(typeof( { ... } ))

and vice versa when writing stuff like

     enum isCopyable(S) = is(typeof( { S foo = S.init; S copy = 
foo; } ));

?

Further, are there cases where the two idioms aren't exchangable?
Sep 20 2017
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Wednesday, 20 September 2017 at 12:01:21 UTC, Nordlöw wrote:
 When is

     __traits(compiles, { ... } )

 preferred over

     is(typeof( { ... } ))

 and vice versa when writing stuff like

     enum isCopyable(S) = is(typeof( { S foo = S.init; S copy = 
 foo; } ));

 ?

 Further, are there cases where the two idioms aren't 
 exchangable?
Yes there are. Prefer __traits(compiles) it includes stricter visibility checks then is() does. Also compiles does not require the expression to resolve to a type.
Sep 20 2017
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 20 September 2017 at 12:20:18 UTC, Stefan Koch 
wrote:
 Yes there are.
 Prefer __traits(compiles) it includes stricter visibility 
 checks then is() does.
1. Can you give example of such visibility checks? 2. If so, should `isCopyable` (along with more defs in std.traits) be converted to instead use `__traits(compiles, )` ? 3. And how do the two idioms differ in terms of compilation-speed?
Sep 20 2017