digitalmars.D - [Spec mistake] Is the `for` loop missing semicolon in the D Lang
- BoQsc (19/19) Aug 09 2023 https://dlang.org/spec/statement.html#for-statement
- Steven Schveighoffer (3/5) Aug 09 2023 NoScopeNonEmptyStatement Contains a semicolon.
- Quirin Schroll (19/22) Aug 09 2023 From the perspective of the parser, the semicolon is part of the
- jmh530 (2/22) Aug 09 2023 That's news to me!
- matheus (6/22) Aug 09 2023 There is no ";" after } but there is after d = 3.14;
- Paul Backus (11/13) Aug 09 2023 Yes, the {} is treated like a block statement, so it doesn't
- Patrick Schluter (6/28) Aug 10 2023 Cool. That one I miss a lot in my C programs. It's not rare to
https://dlang.org/spec/statement.html#for-statement ![Imgur1](https://i.imgur.com/Mw4g87v.png) I tried to test the `for` loop without the first semicolon. It seems that two semicolons are required: compiler produces error. ``` import std; void main() { for (int i = 0 i < 10; i++) writeln("Hello D"); } ``` https://run.dlang.io/is/Pk0wbh ![Imgur2](https://i.imgur.com/bdHJe2Z.png) I'm confused. Do I not understand something in the specification or is it missing a semicolon?
Aug 09 2023
On Wednesday, 9 August 2023 at 11:00:55 UTC, BoQsc wrote:I'm confused. Do I not understand something in the specification or is it missing a semicolon?NoScopeNonEmptyStatement Contains a semicolon. -Steve
Aug 09 2023
On Wednesday, 9 August 2023 at 11:00:55 UTC, BoQsc wrote:[…] I'm confused. Do I not understand something in the specification or is it missing a semicolon?From the perspective of the parser, the semicolon is part of the initialization if it’s a simple one. The initialization can be more complex, though, formed by a braced sequence of statements, where the closing brace suffices to indicate the end of the initialization statement. This is valid D code: ```d void main() { import std.stdio; for ({ int x = 1; double d = 3.14; } x < 10; ++x, d *= 10) { writeln(x, " ", d); } } ``` Notice that the closing brace before `x < 10` has no semicolon following it, and if you place on there, it won’t compile. I’m not saying you should write code like that.
Aug 09 2023
On Wednesday, 9 August 2023 at 13:01:37 UTC, Quirin Schroll wrote:On Wednesday, 9 August 2023 at 11:00:55 UTC, BoQsc wrote:That's news to me![...]From the perspective of the parser, the semicolon is part of the initialization if it’s a simple one. The initialization can be more complex, though, formed by a braced sequence of statements, where the closing brace suffices to indicate the end of the initialization statement. This is valid D code: ```d void main() { import std.stdio; for ({ int x = 1; double d = 3.14; } x < 10; ++x, d *= 10) { writeln(x, " ", d); } } ``` Notice that the closing brace before `x < 10` has no semicolon following it, and if you place on there, it won’t compile. I’m not saying you should write code like that.
Aug 09 2023
On Wednesday, 9 August 2023 at 13:01:37 UTC, Quirin Schroll wrote:On Wednesday, 9 August 2023 at 11:00:55 UTC, BoQsc wrote: ... ```d void main() { import std.stdio; for ({ int x = 1; double d = 3.14; } x < 10; ++x, d *= 10) { writeln(x, " ", d); } } ``` Notice that the closing brace before `x < 10` has no semicolon following it, and if you place on there, it won’t compile. I’m not saying you should write code like that.I wonder if this is a different case or a "bug" in the parser:for ({ int x = 1; double d = 3.14; } x < 10; ++x, d *= 10)There is no ";" after } but there is after d = 3.14; Or maybe {} is treated different in this case, like a block and there is an implicitly ";" being evaluated? Matheus.
Aug 09 2023
On Wednesday, 9 August 2023 at 13:59:54 UTC, matheus wrote:Or maybe {} is treated different in this case, like a block and there is an implicitly ";" being evaluated?Yes, the {} is treated like a block statement, so it doesn't require a semicolon. Compare: import std.stdio; void main() { { writeln("Hello"); } // <-- no semicolon writeln("World"); }
Aug 09 2023
On Wednesday, 9 August 2023 at 13:01:37 UTC, Quirin Schroll wrote:On Wednesday, 9 August 2023 at 11:00:55 UTC, BoQsc wrote:Cool. That one I miss a lot in my C programs. It's not rare to have more than one loop variable of differing types. In C I have to declare one in front of the for loop, which is annoying as the scope of the variable is not the real one I would like. These are the small things I love so much in D.[…] I'm confused. Do I not understand something in the specification or is it missing a semicolon?From the perspective of the parser, the semicolon is part of the initialization if it’s a simple one. The initialization can be more complex, though, formed by a braced sequence of statements, where the closing brace suffices to indicate the end of the initialization statement. This is valid D code: ```d void main() { import std.stdio; for ({ int x = 1; double d = 3.14; } x < 10; ++x, d *= 10) { writeln(x, " ", d); } } ``` Notice that the closing brace before `x < 10` has no semicolon following it, and if you place on there, it won’t compile. I’m not saying you should write code like that.
Aug 10 2023