www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - issue with static foreach

reply someone <someone somewhere.com> writes:
The following code chunk compiles perfectly:

```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;

}
```

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;

}
```

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

... what I am doing wrong ?
Jul 21 2021
next sibling parent reply jfondren <julian.fondren gmail.com> writes:
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: ```d 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.
Jul 21 2021
next sibling parent Basile B. <b2.temp gmx.com> writes:
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: ```d 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.
Jul 22 2021
prev sibling next sibling parent someone <someone somewhere.com> writes:
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 !
Jul 22 2021
prev sibling parent reply Tejas <notrealemail gmail.com> writes:
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: ```d 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? ```d import std; void main() { mixin("int") a; writeln(a); } ```
Jul 22 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Thursday, 22 July 2021 at 17:38:09 UTC, Tejas wrote:
 Why does this work?

 ```d
 import std;
 void main()
 {
     mixin("int") a;
     writeln(a);
 }
 ```
You can mix in a type: https://dlang.org/spec/type.html#mixin_types
Jul 22 2021
parent reply Tejas <notrealemail gmail.com> writes:
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?

 ```d
 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.
Jul 22 2021
parent Paul Backus <snarwin gmail.com> writes:
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?

 ```d
 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.
Jul 22 2021
prev sibling parent reply Patrick Schluter <Patrick.Schluter bbox.fr> writes:
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: ```d 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.
Jul 22 2021
parent someone <someone somewhere.com> writes:
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:


 ```d
 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.
Jul 22 2021