What type does byGrapheme() return?

Steven Schveighoffer schveiguy at gmail.com
Tue Dec 31 14:33:14 UTC 2019


On 12/30/19 6:31 PM, H. S. Teoh wrote:
> On Mon, Dec 30, 2019 at 03:09:58PM -0800, H. S. Teoh via Digitalmars-d-learn wrote:
> [...]
>> I suspect the cause is that whatever Grapheme.opSlice returns is going
>> out-of-scope when used with .map, that's why it's malfunctioning.
> [...]
> 
> Haha, it's actually right there in the Grapheme docs for the opSlice
> overloads:
> 
>          Random-access range over Grapheme's $(CHARACTERS).
> 
>          Warning: Invalidates when this Grapheme leaves the scope,
>          attempts to use it then would lead to memory corruption.
> 
> Looks like when you use .map over the Grapheme, it gets copied into a
> temporary, which gets invalidated when map.front returns.  Somewhere
> we're missing a 'scope' qualifier...
> 

Then the original example should be fixable by putting "ref" in for all 
the lambdas.

But this is kind of disturbing. Why does the grapheme do this? The 
original data is not scoped.

e.g.:

writeln(" Text = ", gr1.map!((ref g) => g[]).joiner.to!string);

-Steve




More information about the Digitalmars-d-learn mailing list