Why foreach(c; someString) must yield dchar

dsimcha dsimcha at yahoo.com
Thu Aug 19 17:13:18 PDT 2010


== Quote from Rainer Deyke (rainerd at eldwood.com)'s article
> On 8/19/2010 03:56, Jonathan Davis wrote:
> > The problem is that chars are not characters. They are UTF-8 code
> > units.
> So what?  You're acting like 'char' (and specifically 'char[]') is some
> sort of unique special case.  In reality, it's just one case of encoded
> data.  What about compressed data?  What about packed arrays of bits?
> What about other containers?
> There's a useful generic idiom for iterating over a sequence of A as if
> it was a sequence of B: the adapter range.  Narrow strings aren't
> special enough to deserve special language support.

Even though I don't agree, I believe this is a reasonable point of view.  The
biggest problem in my mind is that the inconsistency between ranges and foreach
gives us the worst of both worlds when it comes to writing generic code.  While I
think both should use dchar, I'd rather see both use char than have this
ridiculous inconsistency.  I'm waiting for Walter (who has the final say on the
core language) or Andrei (who has the final say on Phobos and ranges) to chime in
on this one.  If Walter insists that we can't make foreach use dchar, then for
consistency's sake at least let's make std.range use char.


More information about the Digitalmars-d mailing list