Thread overview
issue with static foreach
Jul 22, 2021
someone
Jul 22, 2021
jfondren
Jul 22, 2021
Basile B.
Jul 22, 2021
someone
Jul 22, 2021
Tejas
Jul 22, 2021
Paul Backus
Jul 22, 2021
Tejas
Jul 22, 2021
Paul Backus
Jul 22, 2021
Patrick Schluter
Jul 22, 2021
someone
July 22, 2021

The following code chunk compiles perfectly:

labelSwitch: switch (lstrExchangeID) {

static foreach (sstrExchangeID; gstrExchangeIDs) {

   mixin(r"case r"d, `"`, sstrExchangeID, `"`, r"d : "d);
   mixin(r"classTickerCustom"d, sstrExchangeID, r" lobjTicker"d, sstrExchangeID, r" = new classTickerCustom"d, sstrExchangeID, r"(lstrSymbolID);"d);
   //mixin(r"if (true == true) {"d);
   mixin(r"pobjTickersCustom"d, sstrExchangeID, r" ~= lobjTicker"d, sstrExchangeID, r";"d);
   mixin(r"pobjTickersCommon ~= cast(classTickerCommon) lobjTicker"d, sstrExchangeID, r";"d);
   //mixin(r"}"d);
   mixin(r"break labelSwitch;"d);

}

default :

   break;

}

Now, if uncomment those two innocuous commented lines for the if (true == true) block:


labelSwitch: switch (lstrExchangeID) {

static foreach (sstrExchangeID; gstrExchangeIDs) {

   mixin(r"case r"d, `"`, sstrExchangeID, `"`, r"d : "d);
   mixin(r"classTickerCustom"d, sstrExchangeID, r" lobjTicker"d, sstrExchangeID, r" = new classTickerCustom"d, sstrExchangeID, r"(lstrSymbolID);"d);
   mixin(r"if (true == true) {"d);
   mixin(r"pobjTickersCustom"d, sstrExchangeID, r" ~= lobjTicker"d, sstrExchangeID, r";"d);
   mixin(r"pobjTickersCommon ~= cast(classTickerCommon) lobjTicker"d, sstrExchangeID, r";"d);
   mixin(r"}"d);
   mixin(r"break labelSwitch;"d);

}

default :

   break;

}

... it compiles no-more: Error: found End of File when expecting } following compound statement

... what I am doing wrong ?

July 22, 2021

On Thursday, 22 July 2021 at 03:43:44 UTC, someone wrote:

>

... it compiles no-more: Error: found End of File when expecting } following compound statement

... what I am doing wrong ?

You'll get the same error from this code:

unittest {
    mixin("{");
    mixin("}");
}

https://dlang.org/spec/statement.html#mixin-statement

>

The text contents of the string must be compilable as a valid StatementList, and is compiled as such.

Each individual string has to compile on its own. You'll have to concatenate strings and then mixin them.

July 22, 2021

On Thursday, 22 July 2021 at 03:43:44 UTC, someone wrote:

>

Now, if uncomment those two innocuous commented lines for the if (true == true) block:

```d

labelSwitch: switch (lstrExchangeID) {

static foreach (sstrExchangeID; gstrExchangeIDs) {

   mixin(r"case r"d, `"`, sstrExchangeID, `"`, r"d : "d);
   mixin(r"classTickerCustom"d, sstrExchangeID, r" lobjTicker"d, sstrExchangeID, r" = new classTickerCustom"d, sstrExchangeID, r"(lstrSymbolID);"d);
   mixin(r"if (true == true) {"d);
   mixin(r"pobjTickersCustom"d, sstrExchangeID, r" ~= lobjTicker"d, sstrExchangeID, r";"d);
   mixin(r"pobjTickersCommon ~= cast(classTickerCommon) lobjTicker"d, sstrExchangeID, r";"d);
   mixin(r"}"d);
   mixin(r"break labelSwitch;"d);

}

default :

   break;

}

What an unreadable mess. Sorry.

I would have done something like that:

mixin(format!
    `case r"%1$s"d :
       classTickerCustom%1$s  lobjTicker%1$s  = new classTickerCustom%1$s (lstrSymbolID);
       if (true == true) {
           pobjTickersCustom%1$s  ~= lobjTicker%1$s ;
           pobjTickersCommon ~= cast(classTickerCommon) lobjTicker%1$s ;
       }
       break labelSwitch;`(sstrExchangeID)
);

That's easier to edit imho.

July 22, 2021

On Thursday, 22 July 2021 at 05:57:02 UTC, jfondren wrote:

>

On Thursday, 22 July 2021 at 03:43:44 UTC, someone wrote:

>

... it compiles no-more: Error: found End of File when expecting } following compound statement

... what I am doing wrong ?

You'll get the same error from this code:

unittest {
    mixin("{");
    mixin("}");
}

https://dlang.org/spec/statement.html#mixin-statement

>

The text contents of the string must be compilable as a valid StatementList, and is compiled as such.

Each individual string has to compile on its own. You'll have to concatenate strings and then mixin them.

There was a similar issue on bugzilla, and the consensus for closing is that mixins are not supposed to introduce scopes (or unmatched scopes, let's say).

See https://issues.dlang.org/show_bug.cgi?id=3858#c4

However the specs are indeed not up to date with that.

July 22, 2021

On Thursday, 22 July 2021 at 05:57:02 UTC, jfondren wrote:

>

Each individual string has to compile on its own. You'll have to concatenate strings and then mixin them.

I forgot about that !

July 22, 2021

On Thursday, 22 July 2021 at 08:16:43 UTC, Patrick Schluter wrote:

>

What an unreadable mess. Sorry.

Indeed LoL !!!

>

I would have done something like that:

mixin(format!
    `case r"%1$s"d :
       classTickerCustom%1$s  lobjTicker%1$s  = new classTickerCustom%1$s (lstrSymbolID);
       if (true == true) {
           pobjTickersCustom%1$s  ~= lobjTicker%1$s ;
           pobjTickersCommon ~= cast(classTickerCommon) lobjTicker%1$s ;
       }
       break labelSwitch;`(sstrExchangeID)
);

That's easier to edit imho.

No doubt. This happened while refactoring some code with the mixins.

July 22, 2021

On Thursday, 22 July 2021 at 05:57:02 UTC, jfondren wrote:

>

On Thursday, 22 July 2021 at 03:43:44 UTC, someone wrote:

>

... it compiles no-more: Error: found End of File when expecting } following compound statement

... what I am doing wrong ?

You'll get the same error from this code:

unittest {
    mixin("{");
    mixin("}");
}

https://dlang.org/spec/statement.html#mixin-statement

>

The text contents of the string must be compilable as a valid StatementList, and is compiled as such.

Each individual string has to compile on its own. You'll have to concatenate strings and then mixin them.

Why does this work?

import std;
void main()
{
    mixin("int") a;
    writeln(a);
}
July 22, 2021

On Thursday, 22 July 2021 at 17:38:09 UTC, Tejas wrote:

>

Why does this work?

import std;
void main()
{
    mixin("int") a;
    writeln(a);
}

You can mix in a type:

https://dlang.org/spec/type.html#mixin_types

July 22, 2021

On Thursday, 22 July 2021 at 18:06:07 UTC, Paul Backus wrote:

>

On Thursday, 22 July 2021 at 17:38:09 UTC, Tejas wrote:

>

Why does this work?

import std;
void main()
{
    mixin("int") a;
    writeln(a);
}

You can mix in a type:

https://dlang.org/spec/type.html#mixin_types

Looks like special casing to me... is it allowed because they are guaranteed to not introduce a scope, I wonder.

July 22, 2021

On Thursday, 22 July 2021 at 18:16:54 UTC, Tejas wrote:

>

On Thursday, 22 July 2021 at 18:06:07 UTC, Paul Backus wrote:

>

On Thursday, 22 July 2021 at 17:38:09 UTC, Tejas wrote:

>

Why does this work?

import std;
void main()
{
    mixin("int") a;
    writeln(a);
}

You can mix in a type:

https://dlang.org/spec/type.html#mixin_types

Looks like special casing to me... is it allowed because they are guaranteed to not introduce a scope, I wonder.

The list of things you're allowed to mix in is:

  • expressions
  • statements
  • declarations
  • types

In some sense it is special casing, since there's no overarching rule that determines what goes on that list and what doesn't.