www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2532] New: '=' does not give a boolean result

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532

           Summary: '=' does not give a boolean result
           Product: D
           Version: 2.022
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: adolf.mathias googlemail.com


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


-- 
Dec 21 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532





------- Comment #1 from jarrett.billingsley gmail.com  2008-12-21 11:47 -------
= 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.


-- 
Dec 21 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532


2korden gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #2 from 2korden gmail.com  2008-12-21 11:49 -------
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.


-- 
Dec 21 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532





------- Comment #3 from ddparnell bigpond.com  2008-12-21 15:35 -------
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.


-- 
Dec 21 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532





------- Comment #4 from shro8822 vandals.uidaho.edu  2008-12-21 17:46 -------

Might "expression is not a valid condition" be better?


-- 
Dec 21 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532


2korden gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #5 from 2korden gmail.com  2008-12-22 04:35 -------
Reopening as an enhancement.


-- 
Dec 22 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532


gide nwawudu.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic




------- Comment #6 from gide nwawudu.com  2008-12-22 04:40 -------
Added keyword diagnostic.


-- 
Dec 22 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2532





------- Comment #7 from maxmo pochta.ru  2008-12-22 05:55 -------
Hmm... After all D has switched to strict booleans?


-- 
Dec 22 2008