January 15, 2018
On Monday, 15 January 2018 at 06:18:27 UTC, SimonN wrote:
> D's foreach and D's ranges will autodecode and silently iterate over dchar, not char

foreach doesn't do it silently, decoding must be requested from it by explicitly specifying element type, it can also encode this way.
January 15, 2018
On Monday, January 15, 2018 14:56:33 Kagamin via Digitalmars-d-learn wrote:
> On Monday, 15 January 2018 at 06:18:27 UTC, SimonN wrote:
> > D's foreach and D's ranges will autodecode and silently iterate over dchar, not char
>
> foreach doesn't do it silently, decoding must be requested from it by explicitly specifying element type, it can also encode this way.

Yeah, one of the joys of that is that you have to be careful of using foreach in range-based functions, because if you haven't specialized for strings, and you use foreach without specifying the element type, then when your function template is instantiated with a string, the foreach won't match what front does.

I really don't have any complaints about how foreach does this aside from the fact that it doesn't currently use the replacement character (so, it will throw on invalid Unicode if it's told to decode), but the way that interacts with Phobos is poor.

Ideally, we'd get rid of auto-decoding, and we'd get rid of the whole exception on bad Unicode thing and just use the replacement character, but since changing it would break a lot of code... :|

- Jonathan M Davis

January 15, 2018
On Monday, 15 January 2018 at 14:44:46 UTC, Adam D. Ruppe wrote:
> On Monday, 15 January 2018 at 06:18:27 UTC, SimonN wrote:
>> D's foreach [...] will autodecode and silently iterate over dchar, not char, even when the input is string
>
> That's not true. foreach will only decode on demand:
> foreach(c; s) { /* c is a char here, it goes over bytes */ }

Thanks for the correction! Surprised I got foreach(c, s) wrong, its non-decoding iteration is even the prominent example in TDPL.

Even `each`, the template function that implements a foreach, still infers as char:

    "aƤ".each!writeln; // prints a plus two broken characters

Only `map`



When I wrote "D's ranges", I meant Phobos's range-producing templates; a range itself is again encoding-agnostic.
1 2
Next ›   Last »