pu$�le

strtr strtr at sp.am
Sun Jul 18 10:59:21 PDT 2010


== Quote from Jonathan M Davis (jmdavisprog at gmail.com)'s article
> On Sunday 18 July 2010 06:16:09 strtr wrote:
> > I agree with the warning. A good warning would get people to read up on
> > UTF. And if you really want to have char you'll need to cast:
> > foreach(cast(char)c; chars)
> Actually, the cast would be totally unnecessary. Putting
> foreach(char c; chars)
> would be enough. Forcing a cast would change how foreach normally works. I'm not
> even sure that you can legally put a cast there like that. What we'd want to
> disallow would be
> foreach(c; chars)
> As long as the programmer puts the element type, we can assume that they know
> what they're doing. But warning in cases where they don't put it would catch a
> large number of errors in iterating over strings and wstrings.
> In any case, I filed a bug report for it:
> http://d.puremagic.com/issues/show_bug.cgi?id=4483

As a habit I tend to put types everywhere, just recently have I started using auto.
Conceptually, it just looked so obvious foreach(char c; chars) would iterate over
characters.
And you can go on programming like that (in English) for quite a while without
getting any errors whatsoever.
The moment I finally used a single non ascii char I noticed something going wrong
and had to go back and fix quite a few bugs.
And the worst part is, I wasn't the only one making this mistake.
Well, what I wanted to say was that I at least won't assume the programmer knows
what he's doing only because he adds a type.

I totally agree that putting a cast there is probably not really a solution (or
legal).
Warnings for all non-dchar types.
Is there anybody using foreach(c;chars) || foreach(char c;chars) correctly (which
couldn't be done with ubytes)?





More information about the Digitalmars-d-learn mailing list