Thread overview | |||||
---|---|---|---|---|---|
|
July 14, 2013 [Issue 10638] New: Assignment can't be used as a condition | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=10638 Summary: Assignment can't be used as a condition Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: normal Priority: P2 Component: DMD AssignedTo: nobody@puremagic.com ReportedBy: freeslave93@gmail.com --- Comment #0 from Roman <freeslave93@gmail.com> 2013-07-14 04:44:07 PDT --- Suppose code void main() { int i = 0; if (i = 1) { //...... } } dmd compiler generates error: "assignment cannot be used as a condition, perhaps == was meant?" But next code passed successfully: void main() { if (int i = 1) { //...... } } It's a bit odd that assignment can not be used as condition while declaration can be. It works fine if we replace first code snippet with this: void main() { int i = 0; if (cast(bool)(i = 1)) { //...... } } I guess assignment has no implicit cast to bool, it's weird too. If it's not error, please, explain me the reasons of this restriction. Also dlang.org defines ifStatement as "if ( IfCondition ) ThenStatement", where ifCondition can be Expression (hence AssignExpression too), but it seems it does not work at practice. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 14, 2013 [Issue 10638] Assignment can't be used as a condition | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roman | http://d.puremagic.com/issues/show_bug.cgi?id=10638 9999 <mailnew4ster@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mailnew4ster@gmail.com --- Comment #1 from 9999 <mailnew4ster@gmail.com> 2013-07-14 04:48:30 PDT --- I believe the primary reason is to prevent bugs where == was meant and not =. Usually, what you want to do is: void main() { int i = f(); if (i == 1) { //...... } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 14, 2013 [Issue 10638] Assignment can't be used as a condition | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roman | http://d.puremagic.com/issues/show_bug.cgi?id=10638 bearophile_hugs@eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |bearophile_hugs@eml.cc --- Comment #2 from bearophile_hugs@eml.cc 2013-07-14 04:58:13 PDT --- (In reply to comment #1) > I believe the primary reason is to prevent bugs where == was meant and not =. Usually, what you want to do is: > > void main() > { > int i = f(); > if (i == 1) > { > //...... > } > } Right. D is working as designed here, it helps avoid bugs. > Also dlang.org defines ifStatement as "if ( IfCondition ) ThenStatement", > where ifCondition can be Expression (hence AssignExpression too), > but it seems it does not work at practice. Then maybe that's what needs fixing. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation