digitalmars.D.learn - __traits(compiles , mixin ... )
- SrMordred (3/3) Oct 25 2017 Maybe i´m tired already, but whats wrong here:
- SrMordred (7/10) Oct 25 2017 Or the original case I found:
- Adam D. Ruppe (6/8) Oct 25 2017 The semicolon there indicates it is a complete statement that
- SrMordred (3/5) Oct 25 2017 so why this line resolves to false?
- Adam D. Ruppe (11/14) Oct 25 2017 Because it is illegal to put a statement or declaration inside
- SrMordred (4/18) Oct 25 2017 Oh, now thats explains. I thought that a "mixin statement" was
- Adam D. Ruppe (4/6) Oct 25 2017 You are missing a ;
- SrMordred (20/27) Oct 25 2017 it returns false anyway.
Maybe i´m tired already, but whats wrong here: pragma(msg, __traits( compiles, mixin("int x") ) ); //output: false
Oct 25 2017
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: falseOr 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 2017
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 2017
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 2017
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 2017
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:Oh, now thats explains. I thought that a "mixin statement" was equal to the argument type that it compiles to. Thanks!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 2017
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 2017
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: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()` }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 2017