dlang.org icon indicating copy to clipboard operation
dlang.org copied to clipboard

Allow `ref` and `auto ref` in `IfCondition` declarations

Open Bolpat opened this issue 1 week ago • 1 comments

As DIP 1046 (ref For Variable Declarations) has been accepted, probably, ref (and auto ref) should be allowed for the condition in if, while, and switch statements:

int** pptr = …;
if (ref int* ptr = *pptr) …;

Of course, the condition tests whether the variable (ptr in the example) is true-like, not whether the reference is a non-null reference. In the example, it tests ptr !is null, and not &ptr !is null (or, equivalently, pptr !is null).

if (auto ref x = mystery()) …;

Especially in template contexts, auto ref could be used to make x be a ref if mystery() returns by reference, but a value otherwise.

Bolpat avatar Jun 28 '24 14:06 Bolpat