[Spec mistake] Is the `for` loop missing semicolon in the D Lang specification?
Patrick Schluter
Patrick.Schluter at bbox.fr
Thu Aug 10 21:57:08 UTC 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:
>> […]
>> 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.
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.
More information about the Digitalmars-d
mailing list