2532 – '=' does not give a boolean result

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2532 - '=' does not give a boolean result
Summary: '=' does not give a boolean result
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 enhancement
Assignee: No Owner
URL:
Keywords: diagnostic, patch
: 4163 (view as issue list)
Depends on:
Blocks:
Reported: 2008年12月21日 11:04 UTC by Adolf Mathias
Modified: 2015年06月09日 01:20 UTC (History)
3 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Adolf Mathias 2008年12月21日 11:04:45 UTC
Using a boolean assignment in an if condition e.g. as follows gives IMHO an unjustified error message. When a new variable is declared in the if() condition, everything is OK:
// tst.d
void main()
{ bool b;
 if(b=false) { b = true; }
 if(bool c=false) { c = true; }
}
$ dmd tst
tst.d(4): Error: '=' does not give a boolean result
Comment 1 Jarrett Billingsley 2008年12月21日 11:47:07 UTC
= does not give a boolean result for good reason: it's to avoid the common mistake in C/C++ where:
if(x = 5) { ... }
is used when:
if(x == 5) { ... }
was meant.
Comment 2 Koroskin Denis 2008年12月21日 11:49:38 UTC
It works as designed. It is designed to avoid mistakes.
"if (var = expr) {" is disallowed on purpose to avoid mistakes where you inteded to write "if (var == expr) {":
int a = ...;
if (a = 42) { // oops, what a typo! I ment if (a == 42) here...
Bool is no different from int in this expression.
However, "if (type var == expression) {" is not a valid construct, so there is no chance to make a mistake and thus "if (type var = expression) {" is allowed.
There is no 'special cases' for bools.
Comment 3 Derek Parnell 2008年12月21日 15:35:57 UTC
The text of the message is not true if the variable being assigned to is ALREADY a bool. I know that the syntax form 'if (a = b)' is to avoided, but to say that, IN THIS CASE, the result is not a boolean is plainly wrong.
bool a;
bool b;
b = (a = true); // A boolean expression (a = true) is used to assign to 'b'.
As we know that (a = b) is a boolean expression, it seems wrong to say that it is a boolean EXCEPT in an 'if' construct. We do this to no other data type.
So maybe the wording of the message can be changed to reflect what is actually being disallowed. Something like ...
"The syntax form 'if (a = b)' is not allowed".
That takes the argument away from the datatype issue and makes it a syntax issue instead.
Comment 4 BCS 2008年12月21日 17:46:31 UTC
Might "expression is not a valid condition" be better?
Comment 5 Koroskin Denis 2008年12月22日 04:35:05 UTC
Reopening as an enhancement.
Comment 6 Gide Nwawudu 2008年12月22日 04:40:48 UTC
Added keyword diagnostic.
Comment 7 anonymous4 2008年12月22日 05:55:29 UTC
Hmm... After all D has switched to strict booleans?
Comment 8 yebblies 2011年06月13日 09:01:42 UTC
*** Issue 4163 has been marked as a duplicate of this issue. ***
Comment 9 yebblies 2011年06月13日 13:52:46 UTC
https://github.com/D-Programming-Language/dmd/pull/122
A better message for all?
Error: assignment cannot be used as a condition


AltStyle によって変換されたページ (->オリジナル) /