What type does byGrapheme() return?

Steven Schveighoffer schveiguy at gmail.com
Fri Dec 27 17:54:28 UTC 2019


On 12/27/19 12:26 PM, Robert M. Münch wrote:
> I love these documentation lines in the D docs:
> 
>      auto byGrapheme(Range)(Range range)
> 
> How should I know what auto is? Why not write the explicit type so that 
> I know what to expect? When declaring a variable as class/struct member 
> I can't use auto but need the explicit type...
> 
> I used typeof() but that doesn't help a lot:
> 
> gText = [Grapheme(53, 0, 0, 72057594037927936, [83, ...., 1)]Result!string
> 
> I want to iterate a string byGrapheme so that I can add, delete, change 
> graphemes.
> 

This is the rub with ranges. You need to use typeof. There's no other 
way to do it, because the type returned by byGrapheme depends on the 
type of Range.

If you know what type Range is, it would be:

struct S
{
    typeof(string.init.byGrapheme()) gText;
    // or
    alias GRange = typeof(string.init.byGrapheme());
    GRange gText;
}

Subbing in whatever your real range for `string`. Or if it's the result 
of a bunch of adapters, use the whole call chain with typeof.

Why not just declare the real range type? Because it's going to be ugly, 
especially if your underlying range is the result of other range 
algorithms. And really, typeof is going to be the better mechanism, even 
if it's not the best looking thing.

-Steve


More information about the Digitalmars-d-learn mailing list