Confirm: D escapes auto ref similar to Go language
Nick Treleaven
nick at geany.org
Sun Aug 24 21:37:53 UTC 2025
On Sunday, 24 August 2025 at 15:29:01 UTC, Steven Schveighoffer
wrote:
>> ```
>> 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
>> }
>> ```
>
> I don't know what's happening here, but it seems like a bug.
> This should not compile (it does for me, despite the comment
> above).
Yes, I think it's https://github.com/dlang/dmd/issues/19893. If
`parenthesized` is moved above `main`, I get:
```
Error: cannot take address of expression `parenthesized("2 + 3")`
because it is not an lvalue
string * ptrSum = &parenthesized("2 + 3");
^
```
More information about the Digitalmars-d-learn
mailing list