Confirm: D escapes auto ref similar to Go language
Brother Bill
brotherbill at mail.com
Sun Aug 24 15:03:12 UTC 2025
On page 549 of Programming in D, it appears that D supports
'escaping' local variables to the heap, when returning their
address.
This is similar to Go.
Is this what is actually going on?
Is this 'safe' to do?
```
&result: AC067AF730
&result: AC067AF730
ptrSum : AC067AF7B0 (2 + 3)
sum : (4 * 5)
```
source/app.d
```
import std.stdio;
void main() {
string * ptrSum = &parenthesized("2 + 3");
string sum = parenthesized("4 * 5");
writefln("ptrSum : %s %s", ptrSum, *ptrSum);
writefln("sum : %s", sum);
}
auto ref string parenthesized(string phrase) {
string result = '(' ~ phrase ~ ')';
writeln("&result: ", &result);
return result; // ← compilation ERROR
}
```
More information about the Digitalmars-d-learn
mailing list