if(;){} opinion

Sean Kelly sean at f4.ca
Fri Mar 3 09:04:50 PST 2006


Walter Bright wrote:
> "clayasaurus" <clayasaurus at gmail.com> wrote in message 
> news:dtvmkf$2e1a$1 at digitaldaemon.com...
>> I'll vote against it because I don't understand the value of
>>
>> if (m; search("abcdef", "BcdasdfD"))
>>
>> over
>>
>> if (Regexp m = search("abcdef", "BcdasdfD") ) .
>>
>> Maybe someone can enlighten me?
> 
> 1) m is implicitly typed
> 2) looks like the foreach
> 3) more concise 

I've given this some thought and decided I don't see any value in:

     if( m; search( "", "" ) )

over:

     if( auto m = search( "", "" ) )

Both are implicitly typed and I feel the latter is more meaningful.  It 
also has the arguable benefit of being consistent with C++.  Also, I 
think the desire for with foreach is a red herring, as the meaning of 
foreach is entirely unique within D: "for each thing in this sequence, 
do something."  That is, it's clear that a value or value/key pair must 
be assigned or the loop is meaningless.  Contrast this with if, while, 
and even for loops whose purpose is condition evaluation (or perhaps 
counting in the case of a for loop) and the meaning of the declarative 
syntax present in foreach is much less clear.  This also leaves 'for' as 
the odd man out, as I assume it's meaningless to do this:

     for( m; search( "", "" ); )

If this were legal I'd be tempted to suggest simply doing away with 
foreach entirely, which doesn't seem at all reasonable.

So basically, I think that foreach is a special case and should be 
treated as such.  While adding a type name (or 'auto') and an assignment 
may be a bit more verbose than the existing syntax, I think it benefits 
from being far more meaningful in expressions where such assignments are 
not necessary, and does not lose any flexibility over the current method.


Sean



More information about the Digitalmars-d mailing list