Why the hell doesn't foreach decode strings

Jonathan M Davis jmdavisProg at gmx.com
Thu Oct 20 21:06:53 PDT 2011


Actually, I'd have to say that having foreach default to iterating over char 
for string is a bit like if it defaulted to iterating over byte for int[]. 
Sure, it would work in some cases, but in the general case, it would be very 
wrong.

Yes, it's consistent with how arrays are normally iterated over to have 
foreach iterate over char for char[], which is why it's arguably not a good 
idea to make it default to dchar. But it's _wrong_ in most cases, so at least 
giving a warning when the programmer doesn't give an iteration type for 
foreach with an array of char or wchar would be a big help.

It's this very problem that leads some people to argue that string should be 
its own type which holds an array of code units (which can be accessed when 
needed) rather than doing what we do now where we try and treat a string as 
both an array of chars and a range of dchars. The result is schizophrenic.

- Jonathan M Davis


More information about the Digitalmars-d mailing list