First Draft: ref For Variable Declarations
Dukc
ajieskola at gmail.com
Tue Apr 16 10:03:56 UTC 2024
On Monday, 15 April 2024 at 19:21:55 UTC, Steven Schveighoffer
wrote:
> For reference, the edited code which compiles is:
>
> ```d
> @safe ref int foo(return ref int x)
> {
> // ref int y = x;
> // return y;
> // same as:
> ref int bar(ref int x2) {
> return x2;
> }
> return bar(x);
> }
> ```
>
> -Steve
This also answers your original question. You can't do
```D
@safe ref int fun
(return ref int a, return ref int b, return ref int c)
{ // ...
ref int x = b;
// ...
return x;
}
```
...because it uses the same lifetime checks `scope` uses, and you
can't do
```D
@safe int* fun
(return ref int a, return ref int b, return ref int c)
{ // ...
scope int* x = &b;
// ...
return x;
}
```
either. In the second example there is a good reason for the
limitation, as `x` could later be assigned to point to a local
instead.
It could work though. Either `ref`s and immutable/const scope
pointers that don't refer to non-return `scope` could be made
returnable, or we could allow `return ref`/`return scope`/`ref
return scope`/`return ref scope` for local variables too, the
rule being that `return` references can be assigned `return`
references with longer lifetime, but not non-`return` local
references.
More information about the dip.development
mailing list