if (int x=..) but no while(int x=..) ?

Kyle Furlong kylefurlong at gmail.com
Thu Mar 16 15:53:58 PST 2006


kellywilson at nowhere.com wrote:
> Hey Kyle and Lionello,
> 
> In article <dv8b3l$2109$4 at digitaldaemon.com>, Kyle Furlong says...
>> Lionello Lunesu wrote:
>>> I like the fact that we can do
>>>
>>> # if (int recvd = ts.receive(buffer)) {...}
>>>
>>> but is there any reason why this shouldn't apply to "while" also?
>>>
>>> # while (int recvd = ts.receive(buffer)) {...}
>>>
>>> main.d(26): found 'recvd' when expecting '.' following 'int'
>>> main.d(26): found '=' when expecting identifier following 'int.'
>>> main.d(26): found 'ts' when expecting ')'
>>> main.d(26): found ')' when expecting ';' following 'statement' 
>>>
>>>
>> Seems an obvious next step, are there any drawbacks that people can think of?
> 
> 
> PLEASE read the CAVEAT at the end of the post concerning the following rule!
> 
> The grammar for D does not allow a 
> 
> "Declarator = Expression"
> 
> inside the braces of a while statement. The "IfCondition" rule does allow this
> for the if statements, however. Now, can the grammar be modified to accept the
> "Declarator = Expression" rule? Yes, I just modified my grammar and it works
> without conflicts. As for any deeper questions about the
> validity/usability/maintainability of this...well that is for Walter to decide,
> I guess.
> 
> By the way:
> 
> Each of the "IfStatement"/"WhileStatement"/"DoStatement" rules are explained
> with the following in the documentation:
> 
> "Expression is evaluated and must have a type that can be converted to a
> boolean."
> 
> This would seem to indicate that each of these constructs can use the
> "Declarator = Expression" rule I mentioned above.
> 
> Thanks,
> Kelly Wilson 
> 
> CAVEAT:::: The rule "Declarator = Expression" should ACTUALLY read "Basic_Type
> Declarator = Expression". The dmd frontend (version 0.149) uses this rule
> instead of the rule I keep quoting above....I just didn't want to confuse anyone
> following along in the documentation online. I also use the "Basic_Type...."
> rule as the Basic_Type is needed before Declarator.
> 
> 
> 

My point was not that it should be able to support it currently, but that it would be symmetric to support it in a while 
statement as well.



More information about the Digitalmars-d mailing list