First Draft: ref For Variable Declarations

Dennis dkorpel at gmail.com
Mon Apr 15 17:20:59 UTC 2024


On Monday, 15 April 2024 at 16:24:51 UTC, Steven Schveighoffer 
wrote:
> This seems like an unnecessary restriction.

It's necessary to prevent the creation of dangling stack 
pointers, since a `ref` can be bound to a stack variable.

> 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);
> }
> ```

When you fix foo's return type to be `ref int`, you get:

> Error: returning `bar(x)` escapes a reference to parameter `x`




More information about the dip.development mailing list