First Draft: ref For Variable Declarations

Steven Schveighoffer schveiguy at gmail.com
Mon Apr 15 16:24:51 UTC 2024


On Friday, 12 April 2024 at 20:43:50 UTC, Walter Bright wrote:
> https://github.com/WalterBright/documents/blob/984374ca885e1cb10c2667cf872aebc13b4c1663/varRef.md

```
Returning a ref variable from a function that returns a ref is 
not allowed. I.e. a ref cannot be assigned to a ref with a scope 
that exceeds the scope of the source.
```

This seems like an unnecessary restriction. From experience, 
unnecessary restrictions always seem to come back to bite us. 
Yes, it can be added later, but this seems like a no brainer.

1. A ref variable cannot be rebound. Which means that at 
declaration time, you know what it points to and will always 
point to. The lifetime of the new ref variable can assume the 
lifetime of the thing it is assigned to.
2. This would not require flow analysis, since the inputs are 
immutable and known at the time semantic is run for the 
declaration.
3. DIP 1000 already allows such lifetime forwarding:

```d
void foo(ref int x)
{
     // ref int y = x;
     // return y;
     // same as:
     ref int bar(ref int x2) {
         return x2;
     }
     return bar(x);
}
```

Other than that, this looks pretty good!

-Steve


More information about the dip.development mailing list