First Draft: ref For Variable Declarations

Timon Gehr timon.gehr at gmx.ch
Sun May 5 09:46:07 UTC 2024


On 5/2/24 08:58, Walter Bright wrote:
> I forgot to mention: ref's are not rebindable:
> 
> ```
> void test(int i, int j)
> {
>      ref int r = i; // r cannot be rebound to j
> }
> ```
> This has an interesting consequence. The lifetime of the ref will always 
> be less than the lifetime of what it was bound to.
> 

Well, not always, as not every lifetime in a program follows lexical 
scoping.

```d
Array!int a;
a ~= 1;
ref x = a[0];
a.length = 0;
// (`x` still alive here, but `a[0]` is not)
```

(But this is not `@safe` code.)


More information about the dip.development mailing list