What type does byGrapheme() return?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Dec 30 23:38:31 UTC 2019


On Mon, Dec 30, 2019 at 03:31:31PM -0800, H. S. Teoh via Digitalmars-d-learn 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...
[...]

Indeed, compiling with dmd -dip1000 produces this error message:

	test.d(15): Error: returning g.opSlice() escapes a reference to parameter g, perhaps annotate with return
	/usr/src/d/phobos/std/algorithm/iteration.d(499):        instantiated from here: MapResult!(__lambda1, Grapheme[])
	test.d(15):        instantiated from here: map!(Grapheme[])

Not the most helpful message (the annotation has to go in Phobos code,
not in user code), but it does at least point to the cause of the
problem.


T

-- 
What doesn't kill me makes me stranger.


More information about the Digitalmars-d-learn mailing list