Thread overview
bool.d(5): '=' does not give a boolean result
Oct 04, 2004
Regan Heath
Oct 04, 2004
J C Calvarese
Oct 04, 2004
Derek Parnell
Oct 04, 2004
Regan Heath
Oct 04, 2004
Regan Heath
Oct 04, 2004
Derek
Oct 05, 2004
Derek Parnell
Oct 04, 2004
Valéry Croizier
Oct 04, 2004
Regan Heath
October 04, 2004
void main()
{
  bool b;

  if (b = true) {}
}

ok, bad form I know, but the message is a bit.. odd.. right?

Regan

The real-world example read something like

function() {
  foreach() {
    if (!(r = function_call(a,b,c)) break;
  }
  return r;
}

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
October 04, 2004
Regan Heath wrote:
> void main()
> {
>   bool b;
> 
>   if (b = true) {}

Did you mean...

if (b == true) {}

or did the point of this code fly right over my head?

-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
October 04, 2004
On Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese wrote:

> Regan Heath wrote:
>> void main()
>> {
>>   bool b;
>> 
>>   if (b = true) {}
> 
> Did you mean...
> 
> if (b == true) {}
> 
> or did the point of this code fly right over my head?

Maybe, but the error message is

"test.d(5): '=' does not give a boolean result"

The message is "odd" because the variable in question is a bool ;-)

-- 
Derek
Melbourne, Australia
4/10/2004 2:56:56 PM
October 04, 2004
> void main()
> {
>    bool b;
>
>    if (b = true) {}
> }
>
> ok, bad form I know, but the message is a bit.. odd.. right?
>
> Regan

It's not a bug, it's a feature described in the specs : "Assignments do not yield boolean results."



October 04, 2004
On Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese <jcc7@cox.net> wrote:
> Regan Heath wrote:
>> void main()
>> {
>>   bool b;
>>
>>   if (b = true) {}
>
> Did you mean...
>
> if (b == true) {}
>
> or did the point of this code fly right over my head?

It was intended to be an assignment, the real-world code looks something like:

bool function() {
  foreach() {
    if (!(r = function_call(a,b,c)) break;
  }
  return r;
}

I just found it really odd that assigning '=' to a 'bool' doesn't give a boolean result.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
October 04, 2004
On Mon, 4 Oct 2004 14:58:42 +1000, Derek Parnell <derek@psych.ward> wrote:
> On Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese wrote:
>
>> Regan Heath wrote:
>>> void main()
>>> {
>>>   bool b;
>>>
>>>   if (b = true) {}
>>
>> Did you mean...
>>
>> if (b == true) {}
>>
>> or did the point of this code fly right over my head?
>
> Maybe, but the error message is
>
> "test.d(5): '=' does not give a boolean result"
>
> The message is "odd" because the variable in question is a bool ;-)

Yeah that's what I mean't. Assigning to a bool should give a boolean result, yes?
eg.

bool a,b;

a = (b = true);

if (b = true) wasn't a bool, then the above would give an error, right? But it doesn't, instead this

bool r;
bool fn();

if (r = fn()) break;

gives an error.

Basically something seems amiss to me.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
October 04, 2004
On Mon, 4 Oct 2004 23:36:53 +0200, Valéry Croizier <valery@freesurf.fr> wrote:
>> void main()
>> {
>>    bool b;
>>
>>    if (b = true) {}
>> }
>>
>> ok, bad form I know, but the message is a bit.. odd.. right?
>>
>> Regan
>
> It's not a bug, it's a feature described in the specs : "Assignments do not yield boolean results."

Well I'll be.. I didn't see that. :)

While I can understand why this 'feature' is a good idea, it stops mistakes like "if (a=b)" where you meant "if (a==b)" it is a little weird when applied to boolean assignments.

I guess I'll have to re-write:

bool function() {
  foreach() {
    if (!(r = function_call(a,b,c))) break;
  }
  return r;
}

as:

bool function() {
  foreach() {
    r = function_call(a,b,c);
    if (!r) break;
  }
  return r;
}

which is probably a GoodThing(TM) anyway.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
October 04, 2004
On Tue, 05 Oct 2004 10:38:19 +1300, Regan Heath wrote:

> On Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese <jcc7@cox.net> wrote:
>> Regan Heath wrote:
>>> void main()
>>> {
>>>   bool b;
>>>
>>>   if (b = true) {}
>>
>> Did you mean...
>>
>> if (b == true) {}
>>
>> or did the point of this code fly right over my head?
> 
> It was intended to be an assignment, the real-world code looks something like:
> 
> bool function() {
>    foreach() {
>      if (!(r = function_call(a,b,c)) break;
>    }
>    return r;
> }
> 
> I just found it really odd that assigning '=' to a 'bool' doesn't give a boolean result.
> 

This syntax format is a special case that D checks for. Its designed to prevent a common source of bugs in C/C++. The more correct way to code what you want is ...

 bool function() {
    foreach() {
      if ( (r = function_call(a,b,c)) == true) break;
    }
    return r;
 }

In general, the syntax form ...

   if ( a = b )

needs to be coded as ...

   if ( (a = b) != 0 )

The reason is that *most* of the time, what people mean when they code "if (a = b)" is really "if (a == b)". Walter decided to prevent this common coding error. So now if you really really mean to do an assignment /and/ test the result of that assignment, as if it was a boolean test, you must explicitly code the test part of it.

-- 
Derek
Melbourne, Australia
October 05, 2004
On Tue, 5 Oct 2004 07:56:19 +1000, Derek wrote:

> On Tue, 05 Oct 2004 10:38:19 +1300, Regan Heath wrote:
> 
>> On Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese <jcc7@cox.net> wrote:
>>> Regan Heath wrote:
>>>> void main()
>>>> {
>>>>   bool b;
>>>>
>>>>   if (b = true) {}
>>>
>>> Did you mean...
>>>
>>> if (b == true) {}
>>>
>>> or did the point of this code fly right over my head?
>> 
>> It was intended to be an assignment, the real-world code looks something like:
>> 
>> bool function() {
>>    foreach() {
>>      if (!(r = function_call(a,b,c)) break;
>>    }
>>    return r;
>> }
>> 
>> I just found it really odd that assigning '=' to a 'bool' doesn't give a boolean result.
>> 
> 
> This syntax format is a special case that D checks for. Its designed to prevent a common source of bugs in C/C++. The more correct way to code what you want is ...
> 
>  bool function() {
>     foreach() {
>       if ( (r = function_call(a,b,c)) == true) break;
>     }
>     return r;
>  }
> 
> In general, the syntax form ...
> 
>    if ( a = b )
> 
> needs to be coded as ...
> 
>    if ( (a = b) != 0 )
> 
> The reason is that *most* of the time, what people mean when they code "if (a = b)" is really "if (a == b)". Walter decided to prevent this common coding error. So now if you really really mean to do an assignment /and/ test the result of that assignment, as if it was a boolean test, you must explicitly code the test part of it.

Well I suppose it could also be written out thus ...

   a = b;
   if (a) ...

so ...

 bool function() {
     foreach() {
       r = function_call(a,b,c);
       if (r) break;
     }
     return r;
  }

-- 
Derek
Melbourne, Australia
5/10/2004 10:00:29 AM