Confirm: D escapes auto ref similar to Go language
Ali Çehreli
acehreli at yahoo.com
Sun Aug 24 20:43:15 UTC 2025
On 8/24/25 11:17 AM, Brother Bill wrote:
> On Sunday, 24 August 2025 at 17:30:33 UTC, Ali Çehreli wrote:
>> In this case, you used the body of a 'ref' function but compiled it as
>> 'auto ref'. Please remove 'auto' above.
>>
>> Ali
>
> The book has 'auto ref' in "auto ref functions" section.
Yes.
> Does your book need a change?
Not in this case.
> I'm not sure why we should remove 'auto' as you suggest.
The function you show is from this section:
https://ddili.org/ders/d.en/functions_more.html#ix_functions_more.ref,%20return%20type
Note that the function body that you used, the one which has "// ←
compilation ERROR" does not return 'auto ref', but 'ref':
ref string parenthesized(string phrase) {
string result = '(' ~ phrase ~ ')';
return result; // ← compilation ERROR
} // ← the lifetime of result ends here
If you use the function above in your example, meaning, if you remove
'auto' from the code you've shown here, you will get the compilation error.
So, the example in the book is correct.
I think you went a little further in that section and made the return
'auto ref', which does not (and should not) fail compilation.
'ref' functions are different from 'auto ref' functions.
>
> It would be helpful if you would provide a short toy program that has
> the changes you seek.
>
I hope I was clear enough. Start with the program you've shown and
simply remove 'auto' from the function return type. The return type
should be just 'ref'.
Ali
More information about the Digitalmars-d-learn
mailing list