Why can't I give a function's return type the scope storage class?

Meta jared771 at gmail.com
Mon Mar 18 17:22:59 UTC 2019


On Monday, 18 March 2019 at 17:20:37 UTC, Meta wrote:
> On Saturday, 16 March 2019 at 17:59:41 UTC, Walter Bright wrote:
>> On 3/15/2019 10:29 PM, Meta wrote:
>>> Am I wrong in thinking that this is something one would want 
>>> to do? It seems like it would be useful for the callee to 
>>> enforce that its return value is assigned to a scope variable.
>>
>> Scope on function return values comes from the scope of any 
>> arguments passed to the function marked as 'return scope'. 
>> Scope on a function return with no such arguments is currently 
>> meaningless in D.
>>
>> To add such a feature, there woul'd need to be compelling use 
>> cases.
>
> So to be clear, you *can* add scope to the return type of a 
> function, and it will propagate the shortest lifetime among any 
> parameters marked with `return` or `return scope` to the return 
> value? I don't think this is mentioned anywhere in the docs, 
> which is what confused me. I thought that scope on the return 
> type was either a no-op, or that it applied to the function 
> (despite being on the left-hand side). I *think* I can get the 
> effect that I want, given that's how it works. I'll play around 
> a bit and report back.
>
> Also, I'd like to echo the request that you document this 
> stuff. What's already there is fairly useful, but obviously 
> it's not complete.

As an addendum, are these three member function declarations 
equivalent (disregard the fact that this would normally cause a 
duplicate function definition error)?

struct Test
{
     int n = 3;

     @safe
     int* test() return
     {
         return &n;
     }

     @safe
     scope int* test() return
     {
         return &n;
     }

     @safe
     scope int* test() return scope
     {
         return &n;
     }
}



More information about the Digitalmars-d mailing list