Why foreach(c; someString) must yield dchar

dsimcha dsimcha at yahoo.com
Thu Aug 19 07:19:09 PDT 2010

== Quote from Kagamin (spam at here.lot)'s article
> Jonathan Davis Wrote:
> > bytes and shorts are legitimate values on their own, so it wouldn't
> > make sense to give the type to foreach as long.
> Having wider integer always has sense.
> > byte or short on its own just fine.
> Yes, but odds are that it's a bug. You can easily hit an overflow.
> > So, it's almost a guarantee that the correct type for iterating over a
> > string or wstring is dchar, not char or wchar. String types are just
> > weird that way due to how multibyte unicode encodings work.
> If you don't like narrow strings, don't use them. Use dstring. You are free to
write what you want.

One major problem with this is the brokenness of std.string on non-UTF8 strings.
Otherwise this would be a good solution provided you're not dealing with tons of
strings, so space efficiency isn't a major concern.

Hmm, lately I've been focusing my hacking efforts on debugging/polishing/removing
annoying inconsistencies in Phobos.  Maybe std.string should be my next target.
It's generally a frustrating module because in addition to the wide character
issue, lots of stuff requires immutable strings when it could work correctly and
safely with a const or mutable string.

More information about the Digitalmars-d mailing list