borrowed pointers vs ref

Daniel N via Digitalmars-d digitalmars-d at puremagic.com
Mon May 12 14:22:24 PDT 2014


On Monday, 12 May 2014 at 20:36:10 UTC, Walter Bright wrote:
> It's been brought up more than once that the 'scope' storage 
> class is an unimplemented borrowed pointer. But thinking a bit 
> more along those lines, actually 'ref' fills the role of a 
> borrowed pointer.
>
> One particularly apropos behavior is that struct member 
> functions pass 'this' by ref, meaning that members can be 
> called without the inc/dec millstone.
>
> ref is still incomplete as far as this goes, but we can go the 
> extra distance with it, and then it will be of great help in 
> supporting any ref counting solution.
>
> What it doesn't work very well with are class references. But 
> Andrei suggested that we can focus the use of 'scope' to deal 
> with that in an analogous way.
>
> What do you think?
>
> Anyone want to enumerate a list of the current deficiencies of 
> 'ref' in regards to this, so we can think about solving it?

I would prefer 'scope ref' that would allow the solution for 
classes and everything else to be unified, i.e. everything uses 
scope.

When it comes to the implicit 'this' by ref, it could be 
redefined to pass by scope ref.

Another reason is: I know this doesn't(and might never) work in 
D, but based on the intuitive meaning of 'ref' I fully expected 
the below example to work when I first started learning the 
language.

struct A
{
   ref int a_m;
   this(ref int a)
   {
     a_m = a;
   }
}

Whereas 'scope' on the other hand is self documenting imho.


More information about the Digitalmars-d mailing list