Returning a reference to be manipulated

kdevel kdevel at vogtner.de
Fri Apr 14 00:50:31 UTC 2023


On Thursday, 13 April 2023 at 22:09:48 UTC, Jacob Shtokolov wrote:
> [...]
>     ref opIndex(string key) return
> [...]

Regarding the return ref I found this code of 2019 on my harddisk 
which is from or was motivated by a dconf talk:

```
ref int foo (ref int i)
{
    return i;
}

ref int bar ()
{
    int i;
    return foo (i);
}

void main ()
{
    import std.stdio;
    auto i = bar;
    i.writeln;
}
```

Up to dmd v2.100.2 I am warned/get an error during compilation:

```
$ dmd returnref2.d
returnref2.d(3): Deprecation: returning `i` escapes a reference 
to parameter `i`
returnref2.d(1):        perhaps annotate the parameter with 
`return`
$ dmd -dip1000 returnref2.d
returnref2.d(3): Error: returning `i` escapes a reference to 
parameter `i`
returnref2.d(1):        perhaps annotate the parameter with 
`return`
```

With later dmd versions (up to including v2.102.2) the code 
compiles without complaints. Is this intended?


More information about the Digitalmars-d-learn mailing list