Allow `ref` and `auto ref` in `IfCondition` declarations
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.
Thanks for your pull request and interest in making D better, @Bolpat! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Please verify that your PR follows this checklist:
- My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
- My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
- I have provided a detailed rationale explaining my changes
- New or modified functions have Ddoc comments (with
Params:andReturns:)
Please see CONTRIBUTING.md for more information.
If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.
Bugzilla references
Your PR doesn't reference any Bugzilla issue.
If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.
This doesn't account for combinations, these also parse currently:
if (scope ref x = b)
if (const ref x = b)
if (scope const ref x = b)
if (auto scope ref const x = b)
I didn’t know auto and ref allowed other tokens between them. But apparently, they do.
I just found out, while auto and ref commute almost everywhere, they are required in this exact order for lambda expressions; there, they don’t commute. Probably, they shouldn’t commute anywhere as that is hard to understand.
@dkorpel, it it fine? Merge?
The current formulation doesn't allow if (const ref x = y)
The current formulation
What do you mean? The grammar before the PR or the grammar after the latest commit in this PR?
The latter, as I see it, does allow it. The current grammar, of course, does not allow it, but the whole point of the PR is to allow it.
I misread it
No issue. I misread a lot, actually.
I don't think if (ref x = y) actually compiles yet. https://github.com/dlang/dmd/pull/16428 is not merged, and I don't know if it covers that case.
I don't think
if (ref x = y)actually compiles yet. dlang/dmd#16428 is not merged, and I don't know if it covers that case.
The spec technically isn’t supposed to document DMD’s behavior, but what DMD’s behavior ought to be. And it ought to be because the respective DIP was accepted.
@Bolpat I think it's confusing. There should at least be a note that the reference compiler doesn't implement it.
it ought to be because the respective DIP was accepted.
The DIP didn't mention if (ref?
The DIP didn't mention
if (ref?
The DIP is written rather incomplete. It’s of course intended.
I just tried out and if (ref works. if (auto ref does not work, but probably ought to.
Ok, thanks. The dmd pull is merged now anyway.