-
-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend visibility of ref
condition variables to an else
branch
#20823
Comments
if
variables to else
branch if they’re `ref´if
variables to else
branch if they’re ref
if
variables to else
branch if they’re ref
ref
condition variables to an else
branch
|
Yes, intentionally so. IMO, it’s clear insofar as Thinking practically, when one writes code, I fail to see how it would be a problem. The scoping rules make sense intuitively because why would you want to access an I’m not even against extending the scope of an void main()
{
if (int y = 0) { }
else { int y = 0; }
} The only issue I can imagine (and I’ve never seen a forum post about it) is that the current scoping rules make it so in the int y;
void main()
{
if (int y = 0) { }
else { y = 10; }
assert(.y == 10);
} Maybe that shouldn’t be allowed (see #20835).
Because then, the scope of the variable extends beyond the // My suggestion: Stuff just works
void main()
{
if (ref x = f()) { … } else { x = 1; }
if (ref x = g()) { … } else { x = 1; }
} // Your suggestion: Pleasing the compiler
void main()
{
{ ref x = f(); if (x) { … } else { x = 1; } }
{ ref x = g(); if (x) { … } else { x = 1; } }
} |
Current behavior:
That makes sense insofar as
x
is something that evaluated tofalse
which usually narrows its range to a single value, so one replace any use ofx
with0
,false
, ornull
. While not true for user-defined types (which can have anopCast!bool
that returnsfalse
for non-trivial values), it’s still “morally correct” because anyopCast!bool
that returnsfalse
should indicate a trivial value.With
ref
variables, the rationale does not apply. While the referenced value may be trivial, the reference itself isn’t. Assigning it in theelse
branch is meaningful and should be possible.Open questions:
if
variables (for consistency)? (IMO, no)auto ref
variables (for consistency) or only if they’reref
? (IMO, yes)Note that extending by-value
if
variables to theelse
branch is a breaking change, as a local variable declared there may have the same name.The text was updated successfully, but these errors were encountered: