Why foreach(c; someString) must yield dchar

Jonathan M Davis jmdavisprog at gmail.com
Wed Aug 18 20:12:57 PDT 2010


On Wednesday 18 August 2010 19:37:04 dsimcha 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.  Most of it will probably overlook
> this special case in practice, and the net result will be more broken code
> than if we just bite the bullet and fix this now.  Here are some examples
> of the absurdities created by the current situation:
> 
[snip]
> If D is at all serious about generic programming, we simply can't require
> this to be dealt with **everywhere** as a special case.

Considering that in all likelihood 99+% of the cases where someone is iterating 
over char, they really want dchar, I have no problem whatsoever with such a 
change. It may break existing code, but I'd expect that it's more likely to fix 
it. People could still iterate over char or wchar if they want to - they'd just 
have to specify the type. The one thing about it that bugs me is that it means 
that foreach acts differently with chars and wchars then it does with everything 
else, but really, that's a _lot_ less of an issue than the problems that you get 
with generic programming where you have to special case strings all over the 
place.

As I understand it, Walter doesn't want to do this because it silently breaks D1 
code. However, since odds are that that code should have been iterating over 
dchars in the first place, I really think that this change is worth making. In 
light of the costs to generic programming and the fact that programmers the 
world over are going to screw up when using foreach with strings when only a 
bare handful are actually going to want to iterate over chars or wchars, I'd say 
that making this change is worth it. Yes, it may break some existing code, but 
one, I'd expect that it would _fix_ more code than it breaks, and two, this will 
forever be a recurring bug in D programs if it doesn't get fixed. You _know_ that 
the average programmer is going to screw this up and that experienced ones will 
periodically forget to specify the type for foreach and get bittek by it, and 
the cost to generic programming is obviously very high if we leave it as is. So, 
I'd definitely vote to make it so that foreach over chars and wchars defaults to 
dchar. The pain that it will save is _far_ more than the pain that it will cost.

- Jonathan M Davis


More information about the Digitalmars-d mailing list