Defining inout returned values for ranges

anonymous anonymous at example.com
Wed Sep 4 02:49:41 PDT 2013


On Wednesday, 4 September 2013 at 00:56:39 UTC, Jonathan 
Crapuchettes wrote:
> If a range struct (Range) is defined inside another struct 
> (Test), how
> can the constness or mutability of Test be attributed to the 
> return type
> of Range.front? I'm running into this problem because I need 
> the range to
> be iterated, but I need the pointer in T to be marked const when
> appropriate.
>
> Thank you,
> JC
>
> Pseudo-Example:
> struct Test
> {
>     static struct T
>     {
>         uint* ptr;
>     }
>     ...
>
>     auto opSlice() inout
>     {
>         static struct Range
>         {
>             inout(T) front() @property
>             {
>                 ...
>             }
>             ...
>         }
>
>         return Range();
>     }
> }


You can use a Template This Parameter [1] instead of inout:
----
    auto opSlice(this This)()
    {
        static if(is(This == const)) alias QualifiedT = const T;
        else alias QualifiedT = T;

        static struct Range
        {
            QualifiedT front() @property
            {
----

[1] http://dlang.org/template.html#TemplateThisParameter


More information about the Digitalmars-d-learn mailing list