digitalmars.D.bugs - bool.d(5): '=' does not give a boolean result
- Regan Heath (16/16) Oct 03 2004 void main()
- J C Calvarese (7/12) Oct 03 2004 Did you mean...
- Derek Parnell (8/20) Oct 03 2004 Maybe, but the error message is
- Regan Heath (16/32) Oct 04 2004 Yeah that's what I mean't. Assigning to a bool should give a boolean
- Regan Heath (14/23) Oct 04 2004 It was intended to be an assignment, the real-world code looks something...
- Derek (23/50) Oct 04 2004 This syntax format is a special case that D checks for. Its designed to
- Derek Parnell (16/70) Oct 04 2004 Well I suppose it could also be written out thus ...
- Valéry Croizier (2/9) Oct 04 2004 It's not a bug, it's a feature described in the specs : "Assignments do ...
- Regan Heath (25/37) Oct 04 2004 Well I'll be.. I didn't see that. :)
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/
Oct 03 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/
Oct 03 2004
On Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese wrote:Regan Heath wrote: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 PMvoid main() { bool b; if (b = true) {}Did you mean... if (b == true) {} or did the point of this code fly right over my head?
Oct 03 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: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/Regan Heath wrote: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 ;-)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?
Oct 04 2004
On Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese <jcc7 cox.net> wrote:Regan Heath wrote: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/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?
Oct 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: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, AustraliaRegan Heath wrote: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.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?
Oct 04 2004
On Tue, 5 Oct 2004 07:56:19 +1000, Derek wrote:On Tue, 05 Oct 2004 10:38:19 +1300, Regan Heath wrote: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 AMOn Sun, 03 Oct 2004 23:34:42 -0500, J C Calvarese <jcc7 cox.net> wrote: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.Regan Heath wrote: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.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?
Oct 04 2004
void main() { bool b; if (b = true) {} } ok, bad form I know, but the message is a bit.. odd.. right? ReganIt's not a bug, it's a feature described in the specs : "Assignments do not yield boolean results."
Oct 04 2004
On Mon, 4 Oct 2004 23:36:53 +0200, Valéry Croizier <valery freesurf.fr> wrote: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/void main() { bool b; if (b = true) {} } ok, bad form I know, but the message is a bit.. odd.. right? ReganIt's not a bug, it's a feature described in the specs : "Assignments do not yield boolean results."
Oct 04 2004