Why foreach(c; someString) must yield dchar

Simen kjaeraas simen.kjaras at gmail.com
Thu Aug 19 07:51:37 PDT 2010


dsimcha <dsimcha at yahoo.com> wrote:

> I've been hacking in Phobos and parallelfuture and I've come to the  
> conclusion
> that having typeof(c) in the expression foreach(c; string.init) not be a  
> dchar
> is simply ridiculous.  I don't care how much existing code gets broken,  
> this
> needs to be fixed.  Otherwise, all generic code will have to deal with  
> it as a
> special case.

The other alternative, as has been proposed, is to create (d|w)?string
structs that are thin wrappers on top of immutable((d|w)?char)[].

Something along the lines of
struct string {
     immutable(ubyte)[] payload;
     alias payload this;
     // Implement range primitives here.
}
should work, though it is nowhere near as elegant as what we have, and
would probably break some code.

Do note that I used ubyte instead of char as the element type, seeing
as how char/wchar would be unnecessary in this case.


This said, I am more in favor of changing the compiler than the strings.


-- 
Simen


More information about the Digitalmars-d mailing list