www.digitalmars.com         C & C++   DMDScript  

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

reply "cal" <callumenator gmail.com> writes:
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)?
Mar 04 2013
next sibling parent reply "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
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
Mar 05 2013
next sibling parent reply "cal" <callumenator gmail.com> writes:
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 }
Mar 05 2013
next sibling parent reply "simendsjo" <simendsjo gmail.com> writes:
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?
Mar 05 2013
parent "cal" <callumenator gmail.com> writes:
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.
Mar 05 2013
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/5/13, cal <callumenator 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.
Mar 05 2013
prev sibling parent "simendsjo" <simendsjo gmail.com> writes:
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.
Mar 05 2013
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Mar 05 2013
parent "cal" <callumenator gmail.com> writes:
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.
Mar 05 2013