why local variables cannot be ref?
Fanda Vacek
fanda.vacek at gmail.com
Mon Nov 25 08:07:50 UTC 2019
On Monday, 25 November 2019 at 05:51:31 UTC, Rumbu wrote:
> On Monday, 25 November 2019 at 03:07:08 UTC, Fanda Vacek wrote:
>> Maybe I'm missing the thing, but I'm not able to declare local
>> ref variable even if simple workaround exists. 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
>
> Probably you are coming from C#. There are no reference
> variables in D, but you can use pointers like in:
>
> int* b = &a;
> *b = 2;
Thanks for answer, I'm coming from C++. But anyway, pointers are
not allowed in @safe code, so this is not always solution.
Workaround exits even for @safe code, so my question remains the
same. What is a rationale for such a language design restriction,
if it can be workarounded easily.
More information about the Digitalmars-d-learn
mailing list