On 12/31/19 4:22 PM, H. S. Teoh wrote:
On Tue, Dec 31, 2019 at 04:02:47PM -0500, Steven Schveighoffer via
Digitalmars-d-learn wrote:
On 12/31/19 2:58 PM, H. S. Teoh wrote:
On Tue, Dec 31, 2019 at 09:33:14AM -0500, Steven Schveighoffer via
Digitalmars-d-learn wrote:
e.g.:
writeln(" Text = ", gr1.map!((ref g) => g[]).joiner.to!string);
[...]
Unfortunately this doesn't work. Somehow the ref parameter doesn't
match whatever it is std.algorithm.map is trying to pass to it.
Huh, it seemed to work for me. Got the full "Robert" with an R. map
does support ref-ness. Maybe you didn't put ref in the right place?
Here's my full non-working code:
import std;
void main() {
auto x = "Bla\u0301hbla\u0310h\u0309!";
auto r = x.byGrapheme;
writefln("%s", r.map!((ref g) => g[]).joiner.to!string);
}
The compiler says:
/usr/src/d/phobos/std/algorithm/iteration.d(604): Error: template D
main.__lambda1 cannot deduce function from argument types !()(Grapheme),
candidates are:
test.d(5): __lambda1
/usr/src/d/phobos/std/algorithm/iteration.d(499): Error: template
instance test.main.MapResult!(__lambda1, Result!string) error instantiating
test.d(5): instantiated from here: map!(Result!string)
What did I do wrong?
auto r = x.byGrapheme.array;
This is how Robert originally had it if you look a few messages up.
Otherwise, it's not an lvalue.
The fact that a Grapheme's return requires you keep the grapheme in
scope for operations seems completely incorrect and dangerous IMO (note
that operators are going to always have a ref this, even when called on
an rvalue). So even though using ref works, I think the underlying issue
here really is the lifetime problem.
-Steve