www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - __traits(compiles , mixin ... )

reply SrMordred <patric.dexheimer gmail.com> writes:
Maybe i´m tired already, but whats wrong here:

pragma(msg, __traits( compiles, mixin("int x") ) );
//output: false
Oct 25
next sibling parent reply SrMordred <patric.dexheimer gmail.com> writes:
On Wednesday, 25 October 2017 at 19:12:02 UTC, SrMordred wrote:
 Maybe i´m tired already, but whats wrong here:

 pragma(msg, __traits( compiles, mixin("int x") ) );
 //output: false
Or the original case I found: struct T{} pragma(msg, __traits( compiles, T() ) ); //true pragma(msg, __traits( compiles, mixin("T()") ) ); //true mixin( "T();" ); Error: `structliteral` has no effect in expression `T()`
Oct 25
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 25 October 2017 at 19:21:27 UTC, SrMordred wrote:
 mixin( "T();" ); Error: `structliteral` has no effect in 
 expression `T()`
The semicolon there indicates it is a complete statement that does nothing, and that's no error. If there's no ;, it is just an expression that must be somewhere else - and the compile error is deferred until higher in the call chain.
Oct 25
parent reply SrMordred <patric.dexheimer gmail.com> writes:
 The semicolon there indicates it is a complete statement that 
 does nothing, and that's no error.
so why this line resolves to false? void F(){} pragma(msg, __traits( compiles, mixin("F();") ) );//false
Oct 25
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 25 October 2017 at 19:50:31 UTC, SrMordred wrote:
 so why this line resolves to false?
Because it is illegal to put a statement or declaration inside __traits(compiles). sorry, I should have said that before... even though the mixin can be legal in another context, it won't be in the __traits context due to this: https://dlang.org/spec/traits.html#compiles "Returns a bool true if all of the arguments compile (are semantically correct). The arguments can be symbols, types, or expressions that are syntactically correct. The arguments cannot be statements or declarations. " When there's a closing ;, it is a mixin statement.
 void F(){}
 pragma(msg, __traits( compiles, mixin("F();") ) );//false
Oct 25
parent SrMordred <patric.dexheimer gmail.com> writes:
On Wednesday, 25 October 2017 at 20:04:47 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 25 October 2017 at 19:50:31 UTC, SrMordred wrote:
 so why this line resolves to false?
Because it is illegal to put a statement or declaration inside __traits(compiles). sorry, I should have said that before... even though the mixin can be legal in another context, it won't be in the __traits context due to this: https://dlang.org/spec/traits.html#compiles "Returns a bool true if all of the arguments compile (are semantically correct). The arguments can be symbols, types, or expressions that are syntactically correct. The arguments cannot be statements or declarations. " When there's a closing ;, it is a mixin statement.
 void F(){}
 pragma(msg, __traits( compiles, mixin("F();") ) );//false
Oh, now thats explains. I thought that a "mixin statement" was equal to the argument type that it compiles to. Thanks!
Oct 25
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 25 October 2017 at 19:12:02 UTC, SrMordred wrote:
 Maybe i´m tired already, but whats wrong here:

 pragma(msg, __traits( compiles, mixin("int x") ) );
You are missing a ; The mixin must compile as a full thing in context. Variable declarations need the ; to be complete.
Oct 25
parent SrMordred <patric.dexheimer gmail.com> writes:
On Wednesday, 25 October 2017 at 19:25:01 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 25 October 2017 at 19:12:02 UTC, SrMordred wrote:
 Maybe i´m tired already, but whats wrong here:

 pragma(msg, __traits( compiles, mixin("int x") ) );
You are missing a ; The mixin must compile as a full thing in context. Variable declarations need the ; to be complete.
it returns false anyway. void F(){ writeln("inside"); } struct T{} void main() { pragma(msg, __traits( compiles, mixin("int x;") ) );//false pragma(msg, __traits( compiles, mixin("F();") ) );//false pragma(msg, __traits( compiles, mixin("F()") ) );//true mixin( "F();" ); //compiles pragma(msg, __traits( compiles, mixin("T();") ) ); //false pragma(msg, __traits( compiles, mixin("T()") ) ); // true mixin( "T();" ); // Error: `structliteral` has no effect in expression `T()` }
Oct 25