why local variables cannot be ref?
Timon Gehr
timon.gehr at gmx.ch
Mon Nov 25 14:50:58 UTC 2019
On 25.11.19 10:00, Dukc wrote:
> On Monday, 25 November 2019 at 03:07:08 UTC, Fanda Vacek wrote:
>> Is this preferred design pattern?
>>
>> ```
>> int main()
>> {
>> int a = 1;
>> //ref int b = a; // Error: variable `tst_ref.main.b` only
>> parameters or `foreach` declarations can be `ref`
>> ref int b() { return a; }
>> b = 2;
>> assert(a == 2);
>> return 0;
>> }
>> ```
>>
>> Fanda
>
> It's okay, but I'd prefer an alias, because your snippet uses the heap
> needlessly (it puts variable a into heap to make sure there will be no
> stack corruption if you pass a pointer to function b() outside the
> main() function)
This is not true. You can annotate main with @nogc.
More information about the Digitalmars-d-learn
mailing list