Thread overview
__traits(compiles) + mixin
Mar 05, 2013
cal
Mar 05, 2013
Andrej Mitrovic
Mar 05, 2013
cal
Mar 05, 2013
simendsjo
Mar 05, 2013
cal
Mar 05, 2013
Andrej Mitrovic
Mar 05, 2013
simendsjo
Mar 05, 2013
Timon Gehr
Mar 05, 2013
cal
March 05, 2013
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)?
March 05, 2013
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
March 05, 2013
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
}
March 05, 2013
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.
March 05, 2013
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?
March 05, 2013
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.
March 05, 2013
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.

March 05, 2013
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.
March 05, 2013
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.