borrowed pointers vs ref

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Tue May 13 10:18:30 PDT 2014


On Tuesday, 13 May 2014 at 17:09:17 UTC, Steven Schveighoffer 
wrote:
> On Tue, 13 May 2014 09:50:12 -0400, Dicebot <public at dicebot.lv> 
> wrote:
>
>> On Tuesday, 13 May 2014 at 13:40:42 UTC, Jacob Carlborg wrote:
>>> On 13/05/14 15:36, Dicebot wrote:
>>>
>>>> There are 2 `scope` uses to think about. One is storage 
>>>> class and in
>>>> that context `scope` is more of owned / unique pointer. 
>>>> Other is
>>>> parameter qualifier and that one is closer to ref / borrowed 
>>>> pointer.
>>>>
>>>> Main problem about making `ref` borrowed pointer is that you 
>>>> will need
>>>> to prohibit storing it in function transitively. This will 
>>>> need to
>>>> become invalid code:
>>>>
>>>> struct A
>>>> {
>>>>    int* ptr;
>>>> }
>>>>
>>>> int* gptr;
>>>>
>>>> void foo(ref A a)
>>>> {
>>>>    gptr = a.ptr; // error, can't leak borrowed a.ptr into 
>>>> global context
>>>> }
>>>>
>>>> This feels like too much of a breakage, this is why `scope` 
>>>> (or `scope
>>>> ref`) feels more appropriate.
>>>
>>> I always though "scope" would behave like that.
>>
>> Walter's initial post implies that he wanted to re-used `ref` 
>> for borrowed pointer (which would mean same semantics as 
>> `scope` parameter qualifier)
>
> Yes, the difference here is that scope is a storage class, and 
> only affects the "head", whereas borrowed would have to be 
> transitive.
>
> -Steve

`scope` has to be both storage class and qualifier to work


More information about the Digitalmars-d mailing list