evenChunks on a string - hasLength constraint fails?

amarillion mpvaniersel at gmx.com
Thu Mar 16 08:25:24 UTC 2023


On Tuesday, 14 March 2023 at 18:41:50 UTC, Paul Backus wrote:
> On Tuesday, 14 March 2023 at 08:21:00 UTC, amarillion wrote:
>> I'm trying to understand why this doesn't work. I don't really 
>> understand the error. If I interpret this correctly, it's 
>> missing a length attribute on a string, but shouldn't length 
>> be there?
>
> By default, D's standard library treats a `string` as a range 
> of Unicode code points (i.e., a range of `dchar`s), encoded in 
> UTF-8. Because UTF-8 is a variable-length encoding, it's 
> impossible to know how many code points there are in a `string` 
> without iterating it--which means that, as far as the standard 
> library is concerned, `string` does not have a valid `.length` 
> property.
>

Thanks for the clear explanation! I was already aware that you 
could iterate by codepoint with foreach(dchar c; s), but it just 
didn't cross my mind that the same concept was playing a role 
here. I guess it's just one of those things that you just have to 
know.

regards,
Amarillion


More information about the Digitalmars-d-learn mailing list