Feature request: extending comma operator's functionality

Tove tove at fransson.se
Fri Oct 5 00:14:53 PDT 2012


On Friday, 5 October 2012 at 00:22:04 UTC, Jonathan M Davis wrote:
> On Friday, October 05, 2012 02:08:14 bearophile wrote:
>> Tommi:
>> > Maybe we forget about commas then, and extend if-clauses so
>> > that you can properly define variables at the beginning of 
>> > it.
>> > Separated by semicolons.
>> 
>> Regarding definition of variables in D language constructs, 
>> there
>> is one situation where sometimes I find D not handy. This code
>> can't work:
>> 
>> do {
>> const x = ...;
>> } while (predicate(x));
>> 
>> 
>> You need to use:
>> 
>> T x;
>> do {
>> x = ...;
>> } while (predicate(x));

Don't forget the with statement, it's not "just" for switches! In 
many cases it's actually even better than the proposed changes 
_and_ it works today!

import std.stdio;

struct d_is_beautiful
{
   int a=1;
   int b=2;
}

void main()
{
   with(d_is_beautiful()) if(a==1)
     writeln("ok");
   else
     writeln("ko:", a);

   with(d_is_beautiful()) do
   {
     ++a;
     writeln("iter");
   }
   while(a!=b);
}



More information about the Digitalmars-d mailing list