Super-dee-duper D features

renoX renosky at free.fr
Wed Feb 14 13:40:00 PST 2007


Aarti_pl a écrit :
> 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 

The wishlist itself doesn't strike me as very useful:
'on first' can be done with a boolean variable test (maybe a little less 
optimal for performance point of view)
and
'on last' can be done with scope(success){} (at least for the current 
foreach).


Aarti_pl a écrit :
> 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...

Yes! IMHO, this is better than the 'continue foreach': as it avoids the 
'magical' state passing between foreach and continue foreach (ok it's 
not magically passed, i|j are used but as they "look" like simple 
variabl, this still looks weird).
Plus coll1 and coll2 cannot be modified anymore before the second iteration.

My only nitpick would be that the name continue in case(continue) is not 
very good: it could confuse a user to think that this is executed in 
case of usage of the 'normal' continue.
But I can't think of a better name: case(alone), case(only) 
case(iterate_alone),  case(continue_alone) are not very good..
Maybe case(end)(j) (when j do not iterate anymore) to be more consistent 
with case(first), case(last)..

Also what to do when the iteration is made on three collections say 
i,j,k: case(continue)(i,j) ? or maybe case(end)(j,k)?

This would make the code like this:
case(continue)(i,j) { .. foo1 .. }
case(continue)(j,k) { .. foo2 .. }
case(continue)(i,k) { .. foo3 .. }
case(continue)(i) { .. foo4 .. }
case(continue)(j) { .. foo5 .. }
case(continue)(k) { .. foo6 .. }

Hopefully one doesn't need to do this too often!

renoX

PS:
IMHO there are quite a few basic features missing in D such as proper 
associative/static array initialisation, good string format sugar (like 
in Ruby), etc that I have a hard time to find 'continue foreach / 
case(continue)' interesting.



> 
> BR
> Marcin Kuszczak



More information about the Digitalmars-d mailing list