if(;){} opinion

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Sun Feb 26 16:11:37 PST 2006


Walter Bright wrote:
> "Georg Wrede" <georg at nospam.org> wrote in message 
> news:4401ECE3.4030207 at nospam.org...
>> Now consider:
>>
>>         if (Regexp m = std.regexp.search("abcdef", "b(c)d"))
>>         {
>>             writefln("[%s]", m.pre);      // prints [a]
>>             writefln("[%s]", m.post);     // prints [ef]
>>             writefln("[%s]", m.match(0)); // prints [bcd]
>>             writefln("[%s]", m.match(1)); // prints [c]
>>             writefln("[%s]", m.match(2)); // prints []
>>         }
>>
>> Flaunting this around (to both the original programmers, and also to 
>> virgin victims), gave the same, _immediate_ comment from everybody: "Ah, 
>> that's neat!"
> 
> The idea behind m; was to be like foreach. What do the "victims" <g> think 
> of:
> 
> if (auto m = std.regexp.search("abcdef", "b(c)d"))
> 

That makes a lot more sense. Although I prefer

if(type variable, variable = expression, expression == something) {...}

or

if(auto variable = expression, expression == something) {...}

Unfortunately it's currently not possible (at least don't know how to do
it) to declare anything that 'permanent' with the
expression-comma-expression -syntax. I guess the scope ends, when the
next comma is encountered. It would be very nice to support the
comma-notation like above inside if-, while- and for-statements and stop
supporting the current implementation elsewhere. IMO it's pretty useless
outside these three statements.

There's also other use for commas (e.g. initializing multi-dimensional
arrays). I've proposed this before since the syntax for dynamic arrays
is a bit weak at the moment.

-- 
Jari-Matti



More information about the Digitalmars-d mailing list