Re: __traits(compiles , mixin ... )
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!
Re: __traits(compiles , mixin ... )
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
Re: __traits(compiles , mixin ... )
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
Re: __traits(compiles , mixin ... )
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()` }
Re: __traits(compiles , mixin ... )
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.
Re: __traits(compiles , mixin ... )
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()`
Re: __traits(compiles , mixin ... )
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.
__traits(compiles , mixin ... )
Maybe i´m tired already, but whats wrong here: pragma(msg, __traits( compiles, mixin("int x") ) ); //output: false
Re: __traits(compiles) + mixin
On Tuesday, 5 March 2013 at 07:53:15 UTC, cal wrote: I'm confused about this: import std.conv; void main() { enum s = `1`.to!int;; enum c = __traits(compiles, mixin({auto a = new ~s~;})); // line 1 mixin(auto a = ~s~;); // line 2 } This does not compile, giving errors about instantiating std.conv.to!(int).to!(string). If I switch the order of lines 1 and 2 it compiles. Is this a bug or something I don't understand about __traits(compiles)? You can't test declarations inside of __traits(compiles), only expressions. It's in the docs: http://dlang.org/traits.html#compiles
Re: __traits(compiles) + mixin
On Tuesday, 5 March 2013 at 08:04:12 UTC, Andrej Mitrovic wrote: You can't test declarations inside of __traits(compiles), only expressions. It's in the docs: http://dlang.org/traits.html#compiles So why does this work: import std.conv; void main() { enum s = `1`.to!int;; enum c = __traits(compiles, mixin({auto x = ~s~;})); // true }
Re: __traits(compiles) + mixin
On Tuesday, 5 March 2013 at 08:09:37 UTC, cal wrote: On Tuesday, 5 March 2013 at 08:04:12 UTC, Andrej Mitrovic wrote: You can't test declarations inside of __traits(compiles), only expressions. It's in the docs: http://dlang.org/traits.html#compiles So why does this work: import std.conv; void main() { enum s = `1`.to!int;; enum c = __traits(compiles, mixin({auto x = ~s~;})); // true } Hmm.. And this also works: enum c = __traits(compiles, mixin({auto a = new 1;})); Something to do with CTFE combined with mixins?
Re: __traits(compiles) + mixin
On Tuesday, 5 March 2013 at 08:04:12 UTC, Andrej Mitrovic wrote: On Tuesday, 5 March 2013 at 07:53:15 UTC, cal wrote: I'm confused about this: import std.conv; void main() { enum s = `1`.to!int;; enum c = __traits(compiles, mixin({auto a = new ~s~;})); // line 1 mixin(auto a = ~s~;); // line 2 } This does not compile, giving errors about instantiating std.conv.to!(int).to!(string). If I switch the order of lines 1 and 2 it compiles. Is this a bug or something I don't understand about __traits(compiles)? You can't test declarations inside of __traits(compiles), only expressions. It's in the docs: http://dlang.org/traits.html#compiles The errormessage is rather cryptic though.. Makes it look like there is something with conv rather than compiles.
Re: __traits(compiles) + mixin
On 3/5/13, cal callumena...@gmail.com wrote: So why does this work: import std.conv; void main() { enum s = `1`.to!int;; enum c = __traits(compiles, mixin({auto x = ~s~;})); // true } That's a function literal, i.e. an expression.
Re: __traits(compiles) + mixin
On Tuesday, 5 March 2013 at 08:14:58 UTC, simendsjo wrote: Hmm.. And this also works: enum c = __traits(compiles, mixin({auto a = new 1;})); Something to do with CTFE combined with mixins? But it gets this right, in that c is false. I had thought that by wrapping the declaration in braces, making it a function, that got around the 'no declarations' limitation.
Re: __traits(compiles) + mixin
On 03/05/2013 08:53 AM, cal wrote: I'm confused about this: import std.conv; void main() { enum s = `1`.to!int;; enum c = __traits(compiles, mixin({auto a = new ~s~;})); // line 1 mixin(auto a = ~s~;); // line 2 } This does not compile, giving errors about instantiating std.conv.to!(int).to!(string). If I switch the order of lines 1 and 2 it compiles. Is this a bug or something I don't understand about __traits(compiles)? Compiles as expected with DMD 2.060. It is probably a regression.
Re: __traits(compiles) + mixin
On Tuesday, 5 March 2013 at 12:58:57 UTC, Timon Gehr wrote: Compiles as expected with DMD 2.060. It is probably a regression. Ah you're right, also with 2.061. I'll file.
__traits(compiles) + mixin
I'm confused about this: import std.conv; void main() { enum s = `1`.to!int;; enum c = __traits(compiles, mixin({auto a = new ~s~;})); // line 1 mixin(auto a = ~s~;); // line 2 } This does not compile, giving errors about instantiating std.conv.to!(int).to!(string). If I switch the order of lines 1 and 2 it compiles. Is this a bug or something I don't understand about __traits(compiles)?
is(typeof(mixin(X))) and __traits(compiles, mixin(X))
Is there a way to use string mixins with is(typeof(X)) and/or __traits(compiles, X)? As far as I can tell, if I do either of the following is(typeof(mixin(X))) __traits(compiles, mixin(X)) it does not mixin the string and test that for compilation but rather tests whether the mixin expression itself compiles. Is there way to actually test a mixed in string for compilation rather than the mixin expression itself? - Jonathan M Davis
Re: is(typeof(mixin(X))) and __traits(compiles, mixin(X))
On 2010-07-05 10.03, Jonathan M Davis wrote: Is there a way to use string mixins with is(typeof(X)) and/or __traits(compiles, X)? As far as I can tell, if I do either of the following is(typeof(mixin(X))) __traits(compiles, mixin(X)) it does not mixin the string and test that for compilation but rather tests whether the mixin expression itself compiles. Is there way to actually test a mixed in string for compilation rather than the mixin expression itself? - Jonathan M Davis Try to wrap the whole expression in the string mixin. -- Jacob Carlborg