digitalmars.D.learn - issue with static foreach
- someone (44/44) Jul 21 2021 The following code chunk compiles perfectly:
- jfondren (11/16) Jul 21 2021 You'll get the same error from this code:
- Basile B. (6/23) Jul 22 2021 There was a similar issue on bugzilla, and the consensus for
- someone (2/4) Jul 22 2021 I forgot about that !
- Tejas (10/27) Jul 22 2021 Why does this work?
- Paul Backus (3/12) Jul 22 2021 You can mix in a type:
- Tejas (3/16) Jul 22 2021 Looks like special casing to me... is it allowed because they are
- Paul Backus (8/26) Jul 22 2021 The list of things you're allowed to mix in is:
- Patrick Schluter (17/38) Jul 22 2021 What an unreadable mess. Sorry.
- someone (4/20) Jul 22 2021 No doubt. This happened while refactoring some code with the
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
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-statementThe 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
On Thursday, 22 July 2021 at 05:57:02 UTC, jfondren wrote:On Thursday, 22 July 2021 at 03:43:44 UTC, someone wrote: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.... 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-statementThe 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 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 !
Jul 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:Why does this work? ```d import std; void main() { mixin("int") a; writeln(a); } ```... 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-statementThe 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 22 2021
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
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:Looks like special casing to me... is it allowed because they are guaranteed to not introduce a scope, I wonder.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
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: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.On Thursday, 22 July 2021 at 17:38:09 UTC, Tejas wrote:Looks like special casing to me... is it allowed because they are guaranteed to not introduce a scope, I wonder.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
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
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