-
-
Notifications
You must be signed in to change notification settings - Fork 609
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
Allow comparison clause in if
statements which contain statement-local declarations?
#20645
Comments
C++ does something (exactly?) like this right? Or at least there was a proposal to do so. Don't have a link handy at the moment. Will update once I find it. |
C++ has something like this for 10 years or so. |
cppreference tells me C++17 added Presumably this enhancement request also applies to This being a grammar change requires a DIP IIRC. However it shouldn't be too hard to implement, as you note it is a fairly trivial lower from I'll see what I can do. |
We ALREADY support if-clause declarations; the only thing we can't do is specify the condition. If you write: if (auto x = expr) ... What that does right now is check |
I've been wanting a slightly different version of this proposal for a long time: simply allowing multiple assignConditions. Take for example this code from dmd: if (e.e1.type.toBasetype().ty == Tsarray)
if (auto ve = e.e1.isVarExp())
if (auto vd = ve.var.isVarDeclaration())
if (vd.storage_class & STC.ref_)
goal1 = CTFEGoal.LValue; Not only is the deep nesting aesthetically unpleasing, it becomes actually problematic when you want to create an if (e.e1.type.toBasetype().ty == Tsarray;
auto ve = e.e1.isVarExp());
auto vd = ve.var.isVarDeclaration());
vd.storage_class & STC.ref_)
{
goal1 = CTFEGoal.LValue;
}
else
{
} I think this would also allow your use cases, though it does require the variables to be able to cast to
That idea would work for my code example: if (e.e1.type.toBasetype().ty == Tsarray &&
auto ve = e.e1.isVarExp()) &&
auto vd = ve.var.isVarDeclaration()) &&
vd.storage_class & STC.ref_)
{
goal1 = CTFEGoal.LValue;
}
else
{
} And also your example: if (auto x = key in collection && x.isWhatIWant())
{
//...
} (The null check is implicit, because |
initial stab #20653 |
Regarding other control flow statements, I think we should make a subclass of |
I find myself using statement-local declarations in if clauses a lot:
It's natural to want some values used in the determination of the condition to only have a life that matches the if statement.
The problem is that the current language only allows implicit comparison for truth by coersion to
bool
. I'd like to see a second clause added to the if statement which would allow you to write the actual condition after the initialisation of the scope-local variable:The implicit condition written explicitly:
There are really common sequences that cause the current syntax to fail, ie:
Since
in
expressions yield a pointer which may be null in cases where the item doesn't exist, the implicit check just determines if it exists, which is not necessarily the actual condition of interest. There are all kinds of patterns like this which are incompatible with if-statement-local declaration.It feels natural to use semicolon inside of control statements this way; we do it in all the others.
Ie,
for ([declarations]; [condition]; ...)
, it's symmetrical with afor
statement. (just without the increment part)My workaround sometimes looks like this:
...and obviously that's stupid, let's outgrow this.
The text was updated successfully, but these errors were encountered: