www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Bool!

reply Derek Parnell <derek psych.ward> writes:
Damn stupid bool strikes again.

What wrong with this line? ...

  if (util.str.ends(pWord, "'s") == true) return;


Nothing, except that ends() return an integer (-1) if it fails, and the
compiler doesn't bother to mention that it's implicitly converting the bool
(true) to an integer. Not even if -w is operating.

However, if it had bothered to notice I was trying to compare a bool with
an integer, and that might be a dumb idea, I would have been a lot happier.
This hidden bug wasted my time unnecessarily.

Looks like I have to implement a proper boolean type in my code if I want
the compiler to type-check it for me. More waste!

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
22/08/2005 7:49:21 PM
Aug 22 2005
next sibling parent AJG <AJG_member pathlink.com> writes:
In article <4hvh1d33umf2.1jk7qcivm6qvv.dlg 40tude.net>, Derek Parnell says...
Damn stupid bool strikes again.

What wrong with this line? ...

  if (util.str.ends(pWord, "'s") == true) return;


Nothing, except that ends() return an integer (-1) if it fails, and the
compiler doesn't bother to mention that it's implicitly converting the bool
(true) to an integer. Not even if -w is operating.

However, if it had bothered to notice I was trying to compare a bool with
an integer, and that might be a dumb idea, I would have been a lot happier.
This hidden bug wasted my time unnecessarily.

Looks like I have to implement a proper boolean type in my code if I want
the compiler to type-check it for me. More waste!
I second this opinion. Wouldn't it be possible for the compiler to implement bool as an int _internally_ (for "speed") but nevertheless keep it as a separate type for the programmer's benefit? Thanks, --AJG.
Aug 22 2005
prev sibling next sibling parent reply bobef <bobef_member pathlink.com> writes:
I don't see nothing wrong with that. -1 is true. It would be insane if you get
warning everytime you convert int to bool implicitly... Just think how often
something like 'if(foo())' is used where foo is returning int... You know ends()
returns -1 on error so just type it as if (util.str.ends(pWord, "'s") >= 0)
return;

In article <4hvh1d33umf2.1jk7qcivm6qvv.dlg 40tude.net>, Derek Parnell says...
Damn stupid bool strikes again.

What wrong with this line? ...

  if (util.str.ends(pWord, "'s") == true) return;


Nothing, except that ends() return an integer (-1) if it fails, and the
compiler doesn't bother to mention that it's implicitly converting the bool
(true) to an integer. Not even if -w is operating.

However, if it had bothered to notice I was trying to compare a bool with
an integer, and that might be a dumb idea, I would have been a lot happier.
This hidden bug wasted my time unnecessarily.

Looks like I have to implement a proper boolean type in my code if I want
the compiler to type-check it for me. More waste!

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
22/08/2005 7:49:21 PM
Aug 22 2005
next sibling parent reply Sean Kelly <sean f4.ca> writes:
In article <ded68i$js3$1 digitaldaemon.com>, bobef says...
I don't see nothing wrong with that. -1 is true. It would be insane if you get
warning everytime you convert int to bool implicitly... Just think how often
something like 'if(foo())' is used where foo is returning int... You know ends()
returns -1 on error so just type it as if (util.str.ends(pWord, "'s") >= 0)
return;
One could argue that the C convention that D follows is convenient but prone to bugs and misinterpretation. Strictly typed bools are quite useful, though they do result in code being more verbose. Which you prefer is probably a matter of experience more than anything else. Some careful programmers tend to avoid implicit bool conversions however, as they aren't particularly self-documenting. Sean
Aug 22 2005
parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Sean Kelly wrote:
 In article <ded68i$js3$1 digitaldaemon.com>, bobef says...
 
I don't see nothing wrong with that. -1 is true. It would be insane if you get
warning everytime you convert int to bool implicitly... Just think how often
something like 'if(foo())' is used where foo is returning int... You know ends()
returns -1 on error so just type it as if (util.str.ends(pWord, "'s") >= 0)
return;
One could argue that the C convention that D follows is convenient but prone to bugs and misinterpretation. Strictly typed bools are quite useful, though they do result in code being more verbose. Which you prefer is probably a matter of experience more than anything else. Some careful programmers tend to avoid implicit bool conversions however, as they aren't particularly self-documenting. Sean
The problem with implicit conversion between int and bool is that bool (conceptually) is not a subset of (int), or how do I say it? bool to int is not like what float is to double, "bit" is. bool (conceptually) has nothing to do with int what-so-ever, I think asking for a warning is very reasonable. #if (util.str.ends(pWord, "'s") == true) return; This code doesn't even put an "int" expression inside an if statement. It explicitly compares an int to a bool. Unfourtunately, the compiler doesn't think so! bool doesn't even exist in D, it's just an alias for "bit". So the above statement is simply comparing an int to a bit, which is quite resonable in the compiler's opinion.
Aug 22 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 22 Aug 2005 18:41:22 +0000 (UTC), bobef wrote:

 I don't see nothing wrong with that. 
I believe you ;-) Though I think you meant to say "I don't see _anything_ wrong with that." ... not seeing nothing implies that you see something.
-1 is true. 
Well, actually it turns out that -1 is really the integer between -2 and zero. In fact, -1 is really many, many things but one of those is *not* the opposite of 'false'. However, I have heard that some programming language designers had decided to use -1 to represent truth, and others have used 1, and still others had decided that any non-zero value can be used to represent truth. But in each of those cases it was only a representation and nothing more.
It would be insane if you get
 warning everytime you convert int to bool implicitly... 
Funny you say that, I do get people saying I'm a bit insane ;-) Well actually its more like "Lighten up Derek, you're such a pedantic bastard.". But what can I do? - it's my style. I favour clear expression over ambiguity and sloppy coding. That's not to say I don't do sloppy code, but I'm sure as anything that I don't like it.
Just think how often
 something like 'if(foo())' is used where foo is returning int... 
Yes ... scary isn't it? It make you wonder why so many programmers seem to love debugging. Ok, as a concession to some poor historical decisions that would be too costly to correct now, I won't complain (too much) against the idiom "if (EXPRESSION) ...". But I will ask that explicit comparisons of bools with non-bools be one of the things that the compiler can warn us about (-w switch).
You know ends()
 returns -1 on error so just type it as if (util.str.ends(pWord, "'s") >= 0)
 return;
Yes, but I forgot and made a coding mistake that the compiler could have, theoretically, told me about but didn't. It tries so hard to tell me about other coding faux pas but not this type. Weird inconsistency. -- Derek Parnell Melbourne, Australia 23/08/2005 6:55:16 AM
Aug 22 2005
next sibling parent James Dunne <james.jdunne gmail.com> writes:
In article <pweyyzo6qg9v.p5tw00b64723$.dlg 40tude.net>, Derek Parnell says...
On Mon, 22 Aug 2005 18:41:22 +0000 (UTC), bobef wrote:

 I don't see nothing wrong with that. 
I believe you ;-) Though I think you meant to say "I don't see _anything_ wrong with that." ... not seeing nothing implies that you see something.
-1 is true. 
Well, actually it turns out that -1 is really the integer between -2 and zero. In fact, -1 is really many, many things but one of those is *not* the opposite of 'false'. However, I have heard that some programming language designers had decided to use -1 to represent truth, and others have used 1, and still others had decided that any non-zero value can be used to represent truth. But in each of those cases it was only a representation and nothing more.
It would be insane if you get
 warning everytime you convert int to bool implicitly... 
Funny you say that, I do get people saying I'm a bit insane ;-) Well actually its more like "Lighten up Derek, you're such a pedantic bastard.". But what can I do? - it's my style. I favour clear expression over ambiguity and sloppy coding. That's not to say I don't do sloppy code, but I'm sure as anything that I don't like it.
Just think how often
 something like 'if(foo())' is used where foo is returning int... 
Yes ... scary isn't it? It make you wonder why so many programmers seem to love debugging. Ok, as a concession to some poor historical decisions that would be too costly to correct now, I won't complain (too much) against the idiom "if (EXPRESSION) ...". But I will ask that explicit comparisons of bools with non-bools be one of the things that the compiler can warn us about (-w switch).
You know ends()
 returns -1 on error so just type it as if (util.str.ends(pWord, "'s") >= 0)
 return;
Yes, but I forgot and made a coding mistake that the compiler could have, theoretically, told me about but didn't. It tries so hard to tell me about other coding faux pas but not this type. Weird inconsistency. -- Derek Parnell Melbourne, Australia 23/08/2005 6:55:16 AM
Derek, man, where can I pitch up my "I'm a pedantic bastard too" tent?! Amen. (posting me too like some braindead AOLer) Regards, James Dunne
Aug 22 2005
prev sibling parent bobef <bobef_member pathlink.com> writes:
In article <pweyyzo6qg9v.p5tw00b64723$.dlg 40tude.net>, Derek Parnell says...
On Mon, 22 Aug 2005 18:41:22 +0000 (UTC), bobef wrote:

 I don't see nothing wrong with that. 
I believe you ;-) Though I think you meant to say "I don't see _anything_ wrong with that." ... not seeing nothing implies that you see something.
Yes, I see something but it is not wrong. ;] Maybe I meant to say "I see nothing wrong with that". Anyway English is not my native language...
Ok, as a concession to some poor historical decisions that would be too
costly to correct now, I won't complain (too much) against the idiom "if
(EXPRESSION) ...". But I will ask that explicit comparisons of bools with
non-bools be one of the things that the compiler can warn us about (-w
switch).
Sounds good to me. if(EXPRESSION==true) -> warning if(EXPRESSION) -> no warning...
Aug 23 2005
prev sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message 
news:4hvh1d33umf2.1jk7qcivm6qvv.dlg 40tude.net...
 Damn stupid bool strikes again.

 What wrong with this line? ...

  if (util.str.ends(pWord, "'s") == true) return;
I think the design of util.str.ends is just as much at fault as int/bit conversions. I would agree with you that ends should return true/false rather than an index. I say that since the index can be trivially computed from the lengths involved and since the name "ends" doesn't give any clue that it is returning an index. I think you were set up :-)
Aug 22 2005
parent Derek Parnell <derek psych.ward> writes:
On Mon, 22 Aug 2005 19:14:32 -0400, Ben Hinkle wrote:

 "Derek Parnell" <derek psych.ward> wrote in message 
 news:4hvh1d33umf2.1jk7qcivm6qvv.dlg 40tude.net...
 Damn stupid bool strikes again.

 What wrong with this line? ...

  if (util.str.ends(pWord, "'s") == true) return;
I think the design of util.str.ends is just as much at fault as int/bit conversions. I would agree with you that ends should return true/false rather than an index. I say that since the index can be trivially computed from the lengths involved and since the name "ends" doesn't give any clue that it is returning an index. I think you were set up :-)
LOL... I wrote 'ends()'! You may be right about its design. It returns -1 if the first string does not end with the second string, otherwise it returns the length of the first string up to the ending second string. <code> //------------------------------------------------------- int ends(dchar[] pString, dchar[] pSubString) //------------------------------------------------------- { uint lSize; if (pString.length < pSubString.length) return -1; if (pSubString.length == 0) return -1; lSize = pString.length-pSubString.length; if (pString[lSize .. $] != pSubString) return -1; return lSize; } </code> I think I'll amend this design now, but that doesn't change my feeling about bool/non-bool comparisions ;-) -- Derek (skype: derek.j.parnell) Melbourne, Australia 23/08/2005 9:34:46 AM
Aug 22 2005