Super-dee-duper D features

Aarti_pl aarti at interia.pl
Wed Feb 14 03:25:53 PST 2007


Aarti_pl napisał(a):
> Andrei Alexandrescu (See Website For Email) napisał(a):
>>
>> foreach (i ; coll1) (j ; coll2)
>> {
>>   ... use i and j ...
>> }
>> continue foreach (i)
>> {
>>   ... coll2 finished; use i ...
>> }
>> continue foreach (j)
>> {
>>   ... coll1 finished; use j ...
>> }
>>
> 
> Isn't it possible to extend proposed syntax for something like suggested 
> on D wish list:
> 
> http://all-technology.com/eigenpolls/dwishlist/index.php?it=42
> 
> ---
> In short: there is a proposition to add special cases in 'foreach' for 
> first iterated element and last element. It could allow use of 'foreach' 
> also for cases like
> 
> char[] arr = ["a", "b", "c"];
> char[] res;
> for(int i=0; i<arr.length-1; i++) {
>     res~=arr[i] ~ ",";
> }
> if (arr.length>0) res~=arr[$-1];
> 
> into something like this (just raw proposition - syntax might by 
> different):
> 
> foreach(e; arr) {
> res~=e ~ ",";
> }
> last foreach(e) {
> res~=e;
> }
> 
> It seems that it would be logical consequence of proposed syntax... And 
> would be very useful as above use case is quite popular IMHO.
> 
> Best Regards
> Marcin Kuszczak

Maybe syntax like this would be more D-ish, flexible and compact, 
because in fact we need kind of switch..case syntax for 'foreach'... :

foreach (i ; coll1) (j ; coll2) {
	  ... use i and j ...

	case(continue)(i) {
	  ... coll2 finished; use i ...
	}

	case(continue)(j) {
	  ... coll1 finished; use j ...
	}

	case(last)(i) {
	  ... do something with last col1 element
	}

	case(first)(j) {
	  ... do something with first col2 element
	}
}

It does not introduce new keywords and is easy to extend for other 
situations. I like also that everything is a part of foreach, and is not 
separated into different statements...

BR
Marcin Kuszczak



More information about the Digitalmars-d mailing list