Some quick questions

BCS ao at pathlink.com
Tue Aug 7 13:33:06 PDT 2007


Reply to Márcio,

> Hi, I have some quick questions:
> 
> (1.) Why does version 2.0 of the compiler doesn't allow changing the
> index of a foreach loop? For example:
> 
> ;string format = "%s";
> ;
> ;foreach (i, c; format) {
> ;	if (c != '%') {
> ;		// ...
> ;		continue;
> ;	}
> ;
> ;	switch (format[++i]) {
> ;	case 's':
> ;		// ...
> ;		break;
> ;	// ...
> ;	}
> ;}

every time I see the index changed my toes curl. It just seems wrong.

I'd do this:

;string format = "%s";
;int next = 0;
;foreach (i, c; format)
;{
;	if(i != next) continue;
;	if (c != '%')
;	{
;		// ...
;		continue;
;	}
;	next = i + 1;
;	switch (format[next])
;	{
;	case 's':
;		// ...
;		break;
;	// ...
;	}
;}




More information about the Digitalmars-d-learn mailing list