Last update September 24, 2010

Boolean Not Equ Bit



Note:Information in this article is outdated. D now has a native bool type, and no longer has a native bit type (see DMD 0.148 in the change log).

Table of contents of this page
Background   
Discussion   
Some Related D Newsgroup Posts   

Background    

There was a long-lasting debate on the old D Newsgroup (not very surprisingly of a developing language) about the issue of whether the existing bit type really eliminates the need for also having a "true boolean" type. (Historical note: in the development of C++, a similar decades-long debate settled many years ago by adding a native bool type to the language. Nobody really complained afterwards about having bool in the language... )

Note: this is, in some sense, a spectacularly (alarmingly? ) similar thing to not having a "true string" type in the language. Despite the canonical opinion of "we have char[], which (apart from some apparent issues about character indexing with multibyte encoding), does everything a built-in basic string type should do", there is still constant buzz about the missing string type and the request for that pops up almost every week...

To emphasize only one aspect of this debate: if nothing, the opportunity of increasing the expressive power of the language alone should justify adding such basic "extra" features as a bool and a string type. The importance of our ability to express design abstractions, concepts, our ability to map them more directly on language constructs (types) is beyond doubt. If, in this specific case, the language separated, rather than blurred the notions of "machine-level representation type" and "human-level abstract type", that could certainly make almost every designer's life easier.

On the other hand: the language should not have redundant bloat. This is also a very valid concern, even if we do not have agreed definitions of what is redundant and what is bloat. Especially, when bit is almost completely isomorphic to bool -- so why bother? What's more: an "official" bool alias has also been added to the language already, so people of the "Pro-bool" camp can avoid writing bit when they really mean "bool". Now, really why bother?

"bool" alias was added in D 0.69, Aug 11, 2003: "Added bool as an alias for bit."

[About the proper trade-off, let others make their voice heared, too! Someone please add the specific arguments pro/contra! (I just wanted to break the ice here...) -L.K.]

The only problem is that a bit is not a bool . A good paper about this is http://www.python.org/peps/pep-0285.html cited by Mark Evans in my post that you can find below.

List of languages that have a boolean type:

 * Pascal (from the beginning, 1970?)
 * FORTRAN (from FORTRAN 77?)
 * Ada (from the beginning, 1980?)
 * C++ (from the first C++ ISO standard, 199?)
 * Java (from the beginning, 1994)
 * C (from ISO C99 standard)
 * COBOL (from COBOL 2000 standard)
 * C# (from the beginning, 2000?)
 * Python (from version 2.3a0, 2002)
 * Visual Basic 
 * D (from ? )

-R.M.

How many of those languages have a 'bit' type? or even a bool type which specifically does NOT do int arithmetic. The only thing I see different is that repr(x) or toString(x) is a bit shorter than (x?"True":"False").

 -L.C.

Discussion    

IMHO there should be a boolean and a string type, if possible as an alias, if necessary as a separate type. My reasoning is that these are semantically different entities (independent of whether they are technically different), that justify to have a representation in the "language" of the programmer. A larger percentage of the programmers will seek and use these abstractions anyway. -- HelmutLeitner

Posts by Walter


DerekParnell writes: Truth and Falsehood are concepts that are not numeric, and thus math operations using them as data should not be allowed. Statements such as "what is the square root of truth" and "if I have three truths and take away a falsehood, what do I have left", do not make sense. However, if you have a computer language that represents these concepts using an numeric datatype, such as int or bit, one can construct valid code statements that implement these nonsense statements above. And that should not really happen.

If one needs to use a boolean datatype as if it had numeric properties, then one should be able to do that by explictly casting it. For example "cast(int)true" could return an integer value of one, and "cast(int)false" could return an integer value of zero.

Also, I see if expressions of the form "if ( <identifer> )" to be syntax shorthands for "if ( <identfier> != 0 )" and thus should still be allowed. -- DerekParnell


I have no issue whatsoever with the choice of "bit" to represent a bool, I think it makes much more sense than using "char" or "int" like C does. (even if bit is harder to implement, when it comes to pointers and such)

But if true and false are D keywords, then I think bool should be too ? Or go back to the dark ages of C99 and remove keywords true and false too: "module std.stdbool; alias bit bool; const bool true=1; const bool false=0;"

That's my main point: the current selection of keywords is inconsistent! -- AndersFBjörklund

Some Related D Newsgroup Posts    

AuthorDateLinkTitle
Feb 2005
Anders F Björklund10 Feb 2005 NG:digitalmars.D/16281Bits and Bobs and Bools
Oct 2004
larrycowan14 Oct 2004 NG:digitalmars.D/11802Bits again. A proposal.
Anders F Björklund6 Oct 2004 NG:digitalmars.D/11518Too late for boolean ?
Jun 2004
Arcane Jill16 Jun 2004 NG:digitalmars.D/4116More fun with bools
Antti5 Jun 2004 NG:digitalmars.D/3086Imperfect D (A Rant About Bits)
Arcane Jill4 Jun 2004 NG:digitalmars.D/2973Bool: I surrender
Kris3 Jun 2004 NG:digitalmars.D/2908The very last thing(s) I'll say about bool is ...
Arcane Jill1 Jun 2004 NG:digitalmars.D/2683bool
May 2004
Arcane Jill24 May 2004 NG:digitalmars.D/1891Bits
Feb 2004
SpookyET2 Feb 2004 NG:D/24298I want string and bool
Oct 2004
Walter31 Oct 2003 NG:D/18812 
Sep 2004
Matthew Wilson9 Sep 2003 NG:D/16695 
Matthew Wilson8 Sep 2003 NG:D/16651 
John Boucher8 Sep 2003 NG:D/16650 
Matthew Wilson8 Sep 2003 NG:D/16640 
Vathix7 Sep 2003 NG:D/16634 
w7 Sep 2003 NG:D/16633 
Jul 2004
Matthew Wilson11 Jul 2003 NG:D/14374 
Sean L Palmer10 Jul 2003 NG:D/14368 
Matthew Wilson10 Jul 2003 NG:D/14351 
Ilya Minkov9 Jul 2003 NG:D/14341 
Matthew Wilson9 Jul 2003 NG:D/14313 
Matthew Wilson9 Jul 2003 NG:D/14312 
Jun 2003
Sean L Palmer30 Jun 2003 NG:D/14165 
Farmer30 Jun 2003 NG:D/14160 
Sean L Palmer29 Jun 2003 NG:D/14150 
Russ Lewis27 Jun 2003 NG:D/14122 
Bill Cox27 Jun 2003 NG:D/14121 
Jonathan Andrew27 Jun 2003 NG:D/14120 
Sean L Palmer29 Jun 2003 NG:D/14151 
Patrick Down27 Jun 2003 NG:D/14119 
Bill Cox27 Jun 2003 NG:D/14118 
May 2003
J Daniel Smith22 May 2003 NG:D/13524 
Matthew Wilson22 May 2003 NG:D/13506 
Matthew Wilson22 May 2003 NG:D/13505 
Matthew Wilson22 May 2003 NG:D/13503 
Derek Parnell12 May 2003 NG:D/13234 
Helmut Leitner10 May 2003 NG:D/13152 
Mark Evans9 May 2003 NG:D/13122 
Mark Evans9 May 2003 NG:D/13119 
Jonathan Andrew9 May 2003 NG:D/13117 
Mark Evans9 May 2003 NG:D/13115 
Patrick Down9 May 2003 NG:D/13114 
J Daniel Smith9 May 2003 NG:D/13112 
Bill Cox9 May 2003 NG:D/13111 
Helmut Leitner9 May 2003 NG:D/13105 
Mark T9 May 2003 NG:D/13097Appeal to Walter
C Sauls8 May 2003 NG:D/13088 
Feb 2003
Roberto Mariottini14 Feb 2003 NG:D/10953 
Mike Wynn9 Feb 2003 NG:D/10845 
Mark T9 Feb 2003 NG:D/10842 
Jan 2003
Burton Radons27 Jan 2003 NG:D/10524 
Jeroen van Bemmel27 Jan 2003 NG:D/10522bit != bool
Dec 2002
Sab14 Dec 2002 NG:D/9689bool vs. bit or int
Sep 2002
Sandor Hojtsy16 Sep 2002 NG:D/8463 
Dario9 Sep 2002 NG:D/8249 
Luigi8 Sep 2002 NG:D/8242Bit == Boolean??
May 2002
OddesE14 May 2002 NG:D/5117To Bool or not to Bool, that is the question...
Aug 2001
Russ Lewis17 Aug 2001 NG:D/278bool is not an integer?


FolderDiscussions
FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: September 24, 2010 14:45 (diff))