Defining inout returned values for ranges

anonymous anonymous at example.com
Thu Sep 5 14:32:10 PDT 2013


On Thursday, 5 September 2013 at 19:19:42 UTC, Jonathan 
Crapuchettes wrote:
> On Wed, 04 Sep 2013 11:49:41 +0200, anonymous wrote:
[...]
>> 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
>
> Thank you for the help. That worked, but now I ran into another 
> similar
> issue. What if the Range struct is being defined outside of the 
> opSlice
> method? I have a number of methods in a struct that return the 
> same range
> type and would rather not have to have duplicate code or need 
> to use
> mixins.
>
> JC

Templatize Range and pass it This:
----
    static struct Range(This)
    {
        static if(is(This == const)) alias QualifiedT = const T;
        else alias QualifiedT = T;

        QualifiedT front() @property {...}
    }

    auto opSlice(this This)()
    {
        return Range!This();
    }

    ... more methods in the style of opSlice ...
----

If you need QualifiedT in opSlice, make it a template over This, 
too.


More information about the Digitalmars-d-learn mailing list