Super-dee-duper D features

Kirk McDonald kirklin.mcdonald at gmail.com
Tue Feb 13 02:47:20 PST 2007


janderson wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> James Dennett wrote:
>>> C++, of course, has std::for_each(e.begin(), e.end(), do_x);
>>> in its library (though that's weaker than it could be because
>>> of lack of support for convenient anonymous functions/lambdas).
>>>
>>> C++0x is very likely to have for(v: e).  It's implemented
>>> in ConceptGCC already. Java already has essentially that,
>>> as does C#.  This really doesn't set D apart (but at least
>>> D isn't falling behind here).
>>
>> BTW, D might soon have simultaneous iteration that will blow away all 
>> conventional languages:
>>
>> foreach (i ; coll1) (j ; coll2)
>> {
>>   ... use i and j ...
>> }
>> continue foreach (i)
>> {
>>   ... coll2 finished; use i ...
>> }
>> continue foreach (j)
>> {
>>   ... coll1 finished; use j ...
>> }
>>
>> Andrei
> 
> Maybe its a bit to late here however this syntax seems very special 
> case.  Can you explain why its necessary and how we would use it.  How 
> would we do this currently (without meta programming)?
> 
> -Joel

Take the following:

int[] a = [1, 2, 3, 4];
int[] b = [5, 6, 7, 8];

In current D, you can do:

for (int i=0; i<a.length; ++i) {
     writefln("%s %s", a[i], b[i]);
}

With this syntax that Andrei mentions:

foreach (i; a) (j; b) {
     writefln("%s %s", i, j);
}

This has the additional feature of properly handling differently-sized 
collections automatically.

Things get immensely more complicated as soon as you start talking about 
opApply. My sleep-addled brain isn't coming up with a way to do it in 
current D, though I suspect it's possible.

Personally, I think this syntax would be awesome.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org



More information about the Digitalmars-d mailing list