Why is string.front dchar?

bearophile bearophileHUGS at lycos.com
Mon Jan 13 17:12:38 PST 2014


TheFlyingFiddle:

> I'm curious, why is the .front property of narrow strings of 
> type dchar?
> And not the underlying character type for the string.

There was a long discussion on this. It was chosen this way to 
allow most range-based algorithms to work correctly on UTF8 and 
UTF16 strings.

In some cases you can use the std.string.representation function 
to avoid to pay the UTF decoding, or/and to use some algorithms 
as sort().

But for backwards compatibility reasons in this code:

foreach (c; "somestring")

c is a char, not a dchar. You have to type it explicitly to 
handle the UTF safely:

foreach (dchar c; "somestring")

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list