float init 0 request
Walter Bright
newshound2 at digitalmars.com
Thu Aug 20 00:37:53 UTC 2026
On 8/19/2026 2:17 PM, Steven Schveighoffer wrote:
> ```d
> foreach(dchar d; "Über") {
> writeln(d);
> }
>
> foreach(dchar d; "Über".byCodeUnit) {
> writeln(d);
> }
> ```
>
> The first prints 4 code points, each of the characters listed.
>
> The second prints 5 code points, the first two are the *integer promotion* of
> the two utf8 code units that encode "Ü" to dchar, which is nonsense.
It does what it says on the box - gives 5 code units.
The correct code should be:
```d
foreach(dchar d; "Über".byDchar) {
writeln(d);
}
```
byCodeUnit is a building block. The documentation for it says, in part:
" Many characters
are encoded with multiple code units. For example, the UTF-8 code units for
`▒` are `0xC3 0xB8`. That means, an individual element of `byCodeUnit`
often does not form a character on its own. Attempting to treat it as
one while iterating over the resulting range will give nonsensical results."
> The second should not compile. It should be an error to implicitly promote char
> to dchar. It's a nonsense conversion that is allowed implicitly.
Didn't H. S. Teoh argue that char should implicitly convert to wchar?
> Promoting to int is fine. But dchar is not an int.
>
> If you think this example is rare, wait until we remove autodecoding.
More information about the Digitalmars-d
mailing list