WTF?

Robert Fraser fraserofthenight at gmail.com
Sun Mar 16 19:30:27 PDT 2008


Koroskin Denis Wrote:

> On Fri, 14 Mar 2008 05:47:03 +0300, BCS <ao at pathlink.com> wrote:
> 
> > I'm not objecting but...
> >
> > this compiles (1.026):
> >
> > import std.stdio; void main()
> > {
> > 	int j = 3;
> > 	for({ int i; if(j>2) i = 3; else i = 4; } i; i--)
> > 	{
> > 		writef("%d\n", i);
> > 	}
> > }
> >
> >
> 
> 
> It clearly should not, because `i' is out of scope after initialization.  

No it's not. The initialization statement is a non-scoping statement, so if a block statement appears there, its scope is that of the entire for statement.

> Yet, there is a missing semicolon...
> 
> This could be ok:
> for( {}; ; ) {
> 

No it couldn't. If that's okay then either there's a parser bug or something wrong with the spec. I don't have a D compiler on this computer to check, but that shouldn't compile.

According to the grammar (http://www.digitalmars.com/d/2.0/statement.html#ForStatement), a for statement consists of:

for (Initialize Test; Increment) ScopeStatement

Initialize is either a NoScopeNonEmptyStatement, which can be a NonEmptyStatement or a BlockStatement or a ";" (empty statement) if there is no initializer. A { } with or without anything in it is a block statement. It's just confusing since you never see it used that way, and it would probably be bad style to do so.

Under section Statements of the D grammar, it states quite clearly that all offers shall become null and void if - and you can read it for yourself in this interwebbed copy - "I, the undersigned, shall forfeit all rights, privileges, and licenses herein and herein contained," et cetera, et cetera...”Fax mentis incendium gloria cultum," et cetera, et cetera...”Memo bis punitor delicatum!" It's all there, black and white, clear as crystal! You added an extra semicolon. You posted into the newsgroup which now has to be washed and sterilized, so you get nothing! You lose! Good day sir!

Sorry, got a bit carried away with the quoting ;-P.



More information about the Digitalmars-d mailing list