Super-dee-duper D features

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Tue Feb 13 20:04:56 PST 2007


Sean Kelly wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> Kirk McDonald wrote:
>>> Sean Kelly wrote:
>>>> Derek Parnell wrote:
>>>>
>>>>> On Tue, 13 Feb 2007 17:39:46 +0100, Frits van Bommel wrote:
>>>>>
>>>>>> Andrei Alexandrescu (See Website For Email) wrote:
>>>>>>
>>>>>>> Bill Baxter wrote:
>>>>>>>
>>>>>>>> Yeh, I don't get it either.  How would that help me implement 
>>>>>>>> merge() from merge sort for instance?
>>>>>>>
>>>>>>> Merge bumps the iteration in both collections conditionally. The 
>>>>>>> form above bumps the iteration in the two collections 
>>>>>>> unconditionally, until one is finished; then it continues with 
>>>>>>> the other until that is finished.
>>>>>>
>>>>>> In other words, it doesn't :(.
>>>>>
>>>>>
>>>>> I imaging that the full syntax will also include this form ...
>>>>>
>>>>>   foreach (int x, i ; coll1) (int y, j ; coll2)   {
>>>>>    ... use i and j ...
>>>>>      if (somecondition)
>>>>>         x = ...  // To set the index back or forward to some
>>>>>                  // arbitary point in the array 'coll1'.
>>>>>   }
>>>>
>>>>
>>>> This currently works for built-in arrays but not for user-defined 
>>>> types.  Also, I think the fact that it works as all is the result of 
>>>> an implementation detail, not spec-defined behavior.
>>>>
>>>>
>>>> Sean
>>>
>>> There's no reason a user-defined type couldn't implement this.
>>
>> Exactly. Walter and I have bounced a couple of possibilities and 
>> concluded that the feature is of "medium difficulty/medium 
>> usefulness". Probably Walter will look into implementing this first:
>>
>> foreach (i ; 0 .. n)
>> {
>>   ...
>> }
> 
> Out of curiosity, how would these situations be handled:
> 
>   foreach (i ; n .. 0) {}         // A

rewrite to: for (typeof(true ? n : 0) i = n; i < 0; ++i) {}

with the amendment that the loop body can't modify i, and that 0 is 
evaluated only once :o).

>   foreach_reverse (i ; 0 .. n) {} // B

rewrite to: for (typeof(true ? 0 : n) i = n; i-- > 0; ) {}

with the same amendments.

>   foreach_reverse (i ; n .. 0) {} // C

rewrite to: for (typeof(true ? 0 : n) i = 0; i-- > n; ) {}

with the amendment that the loop body can't modify i, and that n is 
evaluated only once.


Andrei



More information about the Digitalmars-d mailing list