www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - WTF?

reply BCS <ao pathlink.com> writes:
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);
	}
}
Mar 13 2008
next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
BCS 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);
     }
 }
I'm assuming that's equivalent to: for(int i = j > 2 ? 3 : 4; 0 != i; i--) ...? If so, it is a bit strange-looking, but the initializer of a for is a statement, so why shouldn't a block statement be allowed there? Grammatically speaking, it'd be more odd if there was some arbitrary rule about for initializers.
Mar 13 2008
parent BCS <ao pathlink.com> writes:
Reply to Robert,

 BCS 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);
 }
 }
I'm assuming that's equivalent to: for(int i = j > 2 ? 3 : 4; 0 != i; i--) ...? If so, it is a bit strange-looking, but the initializer of a for is a statement, so why shouldn't a block statement be allowed there? Grammatically speaking, it'd be more odd if there was some arbitrary rule about for initializers.
If I'd wondered, I have though it would be a declaration or list of declarations. Not a totally arbitrary statement.
Mar 13 2008
prev sibling next sibling parent reply Jascha Wetzel <firstname mainia.de> writes:
BCS 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);
     }
 }
 
 
the specs clearly allow this, since a NoScopeNonEmptyStatement may be a BlockStatement.
Mar 14 2008
parent BCS <ao pathlink.com> writes:
Reply to Jascha,

 BCS 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);
 }
 }
the specs clearly allow this, since a NoScopeNonEmptyStatement may be a BlockStatement.
That's how I found it. I was looking up the empty productions in the other two parts and notices the the second ';' was in the for production, but the first was part of the sub rule and started hunting and noticed what you pointed out. It just seemed strange enough to warrant comment. I don't have a problem with it, but it sure looks odd. <g>
Mar 14 2008
prev sibling parent reply "Koroskin Denis" <2korden gmail.com> writes:
On Fri, 14 Mar 2008 05:47:03 +0300, BCS <ao pathlink.com> wrote:

 I'm not objecting but...

 this compiles (1.026):

 import std.stdio; void main()
 {
 	int j =3D 3;
 	for({ int i; if(j>2) i =3D 3; else i =3D 4; } i; i--)
 	{
 		writef("%d\n", i);
 	}
 }
It clearly should not, because `i' is out of scope after initialization.= = Yet, there is a missing semicolon... This could be ok: for( {}; ; ) { } But not this: for( {} ; ) { } My guess, this is just a lexer bug and was not intended.
Mar 16 2008
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Koroskin Denis Wrote:

 On Fri, 14 Mar 2008 05:47:03 +0300, BCS <ao 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.
Mar 16 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Robert,

 Koroskin Denis Wrote:
 
 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.
it dosn't. [...]
 
 Sorry, got a bit carried away with the quoting ;-P.
 
Man Oh Man do we have a strange type of humor!
Mar 16 2008
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
BCS Wrote:
 Sorry, got a bit carried away with the quoting ;-P.
 
Man Oh Man do we have a strange type of humor!
It's a line from Charlie and the Chocolate Factory (duly modified), poking fun at myself for being so negative in the post. On re-reading, though, it just comes off as me being an ass, especially if you haven't seen the movie. Apologies all around.
Mar 16 2008
parent BCS <BCS pathlink.com> writes:
Robert Fraser wrote:
 BCS Wrote:
 
Sorry, got a bit carried away with the quoting ;-P.
Man Oh Man do we have a strange type of humor!
It's a line from Charlie and the Chocolate Factory (duly modified), poking fun at myself for being so negative in the post. On re-reading, though, it just comes off as me being an ass, especially if you haven't seen the movie. Apologies all around.
Well, I thought it was funny even though I didn't catch the quote. (It did occur to me that it might not be a joke, but that didn't last long) Maybe we should all take to posting MP3's, that way we can send "tone of voice", it would clear up a few things once in a while. :b
Mar 17 2008
prev sibling parent Christopher Wright <dhasenan gmail.com> writes:
Robert Fraser wrote:
 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.
I salute you.
Mar 17 2008