www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Bjarne Stroustrup takes issue with the 'null' keyword

reply Sam <Sam_member pathlink.com> writes:
Should I use NULL or 0?

To quote Bjarne Stroustrup, the god of C++:

"In C++, the definition of NULL is 0, so there is only an aesthetic difference.
I prefer to avoid macros, so I use 0. Another problem with NULL is that people
sometimes mistakenly believe that it is different from 0 and/or not an integer.
In pre-standard code, NULL was/is sometimes defined to something unsuitable and
therefore had/has to be avoided. That's less common these days."

---->  http://www.research.att.com/~bs/bs_faq2.html#null

So as you can see, even the great Bjarne Stroustrup believes we don't need a
'null' keyword.

He also believes that garbage collection was never needed in C++, as
std::auto_ptr<T> can handle everything.
I don't fully agree with this...  But I can't really say he's wrong here either.

I guess my question is: What has D taken from this "so called" wisdom from a "so
called" guru?
May 26 2005
next sibling parent Brad Beveridge <brad somewhere.net> writes:
Sam wrote:
 Should I use NULL or 0?
 
 To quote Bjarne Stroustrup, the god of C++:

D isn't C++ though.
 I guess my question is: What has D taken from this "so called" wisdom from a
"so
 called" guru?

C++ is a complete dog of a language and are looking for something better. That something better is D. In D, null is actually a keyword - not just zero. Due to stronger typing in D, there is an actual distinguishment between null and 0. Brad
May 26 2005
prev sibling next sibling parent reply Sean Kelly <sean f4.ca> writes:
In article <d75g34$6eb$1 digitaldaemon.com>, Sam says...
Should I use NULL or 0?

To quote Bjarne Stroustrup, the god of C++:

"In C++, the definition of NULL is 0, so there is only an aesthetic difference.
I prefer to avoid macros, so I use 0. Another problem with NULL is that people
sometimes mistakenly believe that it is different from 0 and/or not an integer.
In pre-standard code, NULL was/is sometimes defined to something unsuitable and
therefore had/has to be avoided. That's less common these days."

---->  http://www.research.att.com/~bs/bs_faq2.html#null

So as you can see, even the great Bjarne Stroustrup believes we don't need a
'null' keyword.

He also believes that garbage collection was never needed in C++, as
std::auto_ptr<T> can handle everything.
I don't fully agree with this...  But I can't really say he's wrong here either.

I guess my question is: What has D taken from this "so called" wisdom from a "so
called" guru?

FWIW, here is a bit from the C++ standard that discusses NULL: 4.10 - A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4). It's worth noting that this makes zero behave differently than *any other integer value* as only zero is implicitly convertible to a pointer type. It also violates C++ type safety. Besides, which is more meaningful, NULL or 0? If that isn't enough, Bjarne Stroustrup and Herb Sutter proposed a language extension for the next iteration of the C++ standard: the addition of a 'nullptr' constant (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf). In short, I think there's ample support for the existence of 'null' in D :) Sean
May 26 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:d75jdf$930$1 digitaldaemon.com...
 If that isn't enough, Bjarne Stroustrup and Herb Sutter proposed a

 extension for the next iteration of the C++ standard: the addition of a
 'nullptr' constant
 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf).  In

 I think there's ample support for the existence of 'null' in D :)

Some day, C++ will catch up with D <g>.
May 26 2005
next sibling parent imr1984 <imr1984_member pathlink.com> writes:
Not if DMD doesnt build decent debug info it wont. Im very dissappointed that
you still cant view members & globals whilst debugging executables. 

At least C++ has excellent debugging support.

In article <d75uqd$i6n$1 digitaldaemon.com>, Walter says...
"Sean Kelly" <sean f4.ca> wrote in message
news:d75jdf$930$1 digitaldaemon.com...
 If that isn't enough, Bjarne Stroustrup and Herb Sutter proposed a

 extension for the next iteration of the C++ standard: the addition of a
 'nullptr' constant
 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf).  In

 I think there's ample support for the existence of 'null' in D :)

Some day, C++ will catch up with D <g>.

May 27 2005
prev sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:d75uqd$i6n$1 digitaldaemon.com...
 "Sean Kelly" <sean f4.ca> wrote in message
 news:d75jdf$930$1 digitaldaemon.com...
 If that isn't enough, Bjarne Stroustrup and Herb Sutter proposed a

 extension for the next iteration of the C++ standard: the addition of a
 'nullptr' constant
 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf).  In

 I think there's ample support for the existence of 'null' in D :)

Some day, C++ will catch up with D <g>.

It already did, several years ago: #include <stlsoft_nulldef.h>
May 27 2005
prev sibling next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Sam wrote:
 Should I use NULL or 0?
 
 To quote Bjarne Stroustrup, the god of C++:
 
 "In C++, the definition of NULL is 0, so there is only an aesthetic difference.
 I prefer to avoid macros, so I use 0. Another problem with NULL is that people
 sometimes mistakenly believe that it is different from 0 and/or not an integer.
 In pre-standard code, NULL was/is sometimes defined to something unsuitable and
 therefore had/has to be avoided. That's less common these days."
 
 ---->  http://www.research.att.com/~bs/bs_faq2.html#null
 
 So as you can see, even the great Bjarne Stroustrup believes we don't need a
 'null' keyword.
 
 He also believes that garbage collection was never needed in C++, as
 std::auto_ptr<T> can handle everything.
 I don't fully agree with this...  But I can't really say he's wrong here
either.
 
 I guess my question is: What has D taken from this "so called" wisdom from a
"so
 called" guru?
 
 

That's because of a _fault_ in C++, where you can compare pointers against numbers.. the consequence is that a null pointer becomes a 0 pointer. But that's just silly, pointers are *not* numbers.
May 26 2005
parent reply Sam <Sam_member pathlink.com> writes:
Pointers are numbers!

0x00000000 is the null pointer in C++, that's why we can use 0 instead of null.
And Bjarne has stated that this hasn't changed on any machine that he knows of,
and probably won't ever change.

If I have a pointer p (in C++) to an array of 3 ints, and p = 0xffffff20,
then to access the second item in the array I can do:

int i = *(p + 1) // 0xffffff21

So if you can do arithmetic on pointers they must be numbers!  Right?

That's because of a _fault_ in C++, where you can compare pointers 
against numbers.. the consequence is that a null pointer becomes a 0 
pointer.

But that's just silly, pointers are *not* numbers.

May 26 2005
next sibling parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Sam wrote:
 Pointers are numbers!

Physically: yes. Conceptually: no. -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
May 26 2005
prev sibling next sibling parent Hasan Aljudy <Hasan_member pathlink.com> writes:
I guess you're right there, but that's pointers.
D also has references, and I think you can't do arithmetic on references.
Pointers are kinda low level, but references are not so low, if I'm not wrong,
references are a higher level abstraction of pointers, where they are no longer
memory addresses, but object references. They encapsulate the concept of a
reference, and hide the concept of memorry addresses.

In article <d75o3q$caa$1 digitaldaemon.com>, Sam says...
Pointers are numbers!

0x00000000 is the null pointer in C++, that's why we can use 0 instead of null.
And Bjarne has stated that this hasn't changed on any machine that he knows of,
and probably won't ever change.

If I have a pointer p (in C++) to an array of 3 ints, and p = 0xffffff20,
then to access the second item in the array I can do:

int i = *(p + 1) // 0xffffff21

So if you can do arithmetic on pointers they must be numbers!  Right?

That's because of a _fault_ in C++, where you can compare pointers 
against numbers.. the consequence is that a null pointer becomes a 0 
pointer.

But that's just silly, pointers are *not* numbers.


May 26 2005
prev sibling parent Matthias Becker <Matthias_member pathlink.com> writes:
Pointers are numbers!

Well, text is represented as numbers as well. But it's stupid to think about text as numbers. Instead you should simply read it as text. So you should think about pointers as something that stores positions of objects not as numbers.
0x00000000 is the null pointer in C++, that's why we can use 0 instead of null.
And Bjarne has stated that this hasn't changed on any machine that he knows of,
and probably won't ever change.

If I have a pointer p (in C++) to an array of 3 ints, and p = 0xffffff20,
then to access the second item in the array I can do:

int i = *(p + 1) // 0xffffff21

So if you can do arithmetic on pointers they must be numbers!  Right?

Wrong. pointer + pointer => error If you can't even add them you can't say they are numbers (ever seen a number that can't be added to another one? There are even languages that allow things like string * number to repeat a string several times. So are strings numbers? I don't think so.
May 27 2005
prev sibling next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Sam" <Sam_member pathlink.com> wrote in message 
news:d75g34$6eb$1 digitaldaemon.com...
 Should I use NULL or 0?

 To quote Bjarne Stroustrup, the god of C++:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf
May 26 2005
next sibling parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Ben Hinkle" <ben.hinkle gmail.com> wrote in message 
news:d75oct$chc$1 digitaldaemon.com...
 "Sam" <Sam_member pathlink.com> wrote in message 
 news:d75g34$6eb$1 digitaldaemon.com...
 Should I use NULL or 0?

 To quote Bjarne Stroustrup, the god of C++:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf

oops - didn't see Sean had already linked to that. sorry!
May 26 2005
prev sibling parent Matthias Becker <Matthias_member pathlink.com> writes:
In article <d75oct$chc$1 digitaldaemon.com>, Ben Hinkle says...
"Sam" <Sam_member pathlink.com> wrote in message 
news:d75g34$6eb$1 digitaldaemon.com...
 Should I use NULL or 0?

 To quote Bjarne Stroustrup, the god of C++:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf

or http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1601.pdf
May 27 2005
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Sam" <Sam_member pathlink.com> wrote in message 
news:d75g34$6eb$1 digitaldaemon.com...

This is .. quite simply the most inane argument I've seen anyone make on any 
forum or NG in a long time.  The last time I saw something this crazy, 
someone was arguing over the proper name for a style of 3d shading, and he 
was arguing the wrong name.

You want to remove something as simple, innocuous, and commonly-used as the 
"null" keyword?

What would you do if "null" weren't a keyword?  I'm sure there's a lot of 
opportunity opened up for variable and function names by removing that pesky 
restriction.

You said something about "the fewer keywords a language has, the easier it 
is to learn."  That is .. the second most inane argument I've ever heard. 
The number of keywords has nothing to do with the programming style, runtime 
library, under-the-hood functionality, structure, or overall complexity of 
the language.  "true," "false," and "null" are simply there to increase 
readability, and because it conceptually makes no sense to compare a class 
reference to 0.  New programmers shouldn't have to understand the underlying 
concepts of memory addresses in a language like D, and shouldn't have to 
know that address 0 means "nothing."  If anything, having "null" makes it 
easier to learn the language.

You know what I can't stand?  That pesky "if" keyword.  Why can't we just 
use the ?: operator everywhere?  The fewer keywords, the easier, right?  :P 
May 26 2005
next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
 You know what I can't stand?  That pesky "if" keyword.  Why can't we just 
 use the ?: operator everywhere?  The fewer keywords, the easier, right? 
 :P

yeah! - now you're gettin' in the spirit!
May 26 2005
next sibling parent clayasaurus <clayasaurus gmail.com> writes:
Ben Hinkle wrote:
You know what I can't stand?  That pesky "if" keyword.  Why can't we just 
use the ?: operator everywhere?  The fewer keywords, the easier, right? 
:P

yeah! - now you're gettin' in the spirit!

and we can replace for, foreach, and while loops with goto :P
May 26 2005
prev sibling next sibling parent "Kris" <fu bar.com> writes:
You know what bothers me?  All those darned extra button on this keyboard
thingy ... why can't we program using only the space-bar and ctrl-key?


"Ben Hinkle" <ben.hinkle gmail.com> wrote in message
news:d75ug9$i2a$1 digitaldaemon.com...
 You know what I can't stand?  That pesky "if" keyword.  Why can't we


 use the ?: operator everywhere?  The fewer keywords, the easier, right?
 :P

yeah! - now you're gettin' in the spirit!

May 26 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 26 May 2005 21:52:07 -0400, Ben Hinkle wrote:

 You know what I can't stand?  That pesky "if" keyword.  Why can't we just 
 use the ?: operator everywhere?  The fewer keywords, the easier, right? 
 :P

yeah! - now you're gettin' in the spirit!

I once used a notation that went a bit like this ... (? name = "derek" <T> (amt := 10) <F> (amt := 1) ) Weird at first, but I got used to it quickly. -- Derek Melbourne, Australia 27/05/2005 12:14:06 PM
May 26 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Derek Parnell wrote:
 I once used a notation that went a bit like this ...
 
   (? name = "derek"
      <T> (amt := 10)
      <F> (amt := 1)
   )
 
 Weird at first, but I got used to it quickly.

Looks semi-Lisp-ish... or maybe Guile/Scheme... what on earth was/is it? Although I did once use something similar: # ? (name == "derek") # : amt = 10 # ; amt = 1 # . And yes that period (.) does go at the end... and if either of the clauses, seperated by a semi-colon (;) had multiple statements... they were seperated by a comma (,)... cute, huh? I think demons invented it. -- Chris Sauls
May 26 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 26 May 2005 21:33:14 -0500, Chris Sauls wrote:

 Derek Parnell wrote:
 I once used a notation that went a bit like this ...
 
   (? name = "derek"
      <T> (amt := 10)
      <F> (amt := 1)
   )
 
 Weird at first, but I got used to it quickly.

Looks semi-Lisp-ish... or maybe Guile/Scheme... what on earth was/is it?

[embarrased] It was a simple scripting language that I developed when working with IBM 360 mainframes back in the 1970s. I was playing around with Lisp, PL/I, Fortran, and Autocoder then. Seemed like a good idea at the time ;-) It could do really bad stuff ... (? amt < lt 10 > ( f := '+= 10') ; add 10 when < 10 < le 20 > ( f := '+= 1') ; add 1 when between 10 and 20 < > ( f := '+= 5') ; otherwise add 5 ) (amt $f) ; apply formula -- Derek Melbourne, Australia 27/05/2005 12:31:39 PM
May 26 2005
parent Chris Sauls <ibisbasenji gmail.com> writes:
Derek Parnell wrote:
    (? amt 
       < lt 10 > ( f := '+= 10') ; add 10 when < 10
       < le 20 > ( f := '+= 1') ; add 1 when between 10 and 20
       < > ( f := '+= 5') ; otherwise add 5
    )
    (amt $f) ; apply formula 
 

Ahh.. so 'f' contained an "arbitrary" expression to be evaluated in terms of a variable via a '(xxx $f)' expression... creative. As a friend of mine once said of one of his student's code... "Cute. Not wise... but cute." :) *steals the idea* -- Chris Sauls
May 26 2005
prev sibling next sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Jarrett Billingsley wrote:
 "Sam" <Sam_member pathlink.com> wrote in message 
 news:d75g34$6eb$1 digitaldaemon.com...
 
 This is .. quite simply the most inane argument I've seen anyone make on any 
 forum or NG in a long time.  The last time I saw something this crazy, 
 someone was arguing over the proper name for a style of 3d shading, and he 
 was arguing the wrong name.
 
 You want to remove something as simple, innocuous, and commonly-used as the 
 "null" keyword?
 
 What would you do if "null" weren't a keyword?  I'm sure there's a lot of 
 opportunity opened up for variable and function names by removing that pesky 
 restriction.
 
 You said something about "the fewer keywords a language has, the easier it 
 is to learn."  That is .. the second most inane argument I've ever heard. 
 The number of keywords has nothing to do with the programming style, runtime 
 library, under-the-hood functionality, structure, or overall complexity of 
 the language.  "true," "false," and "null" are simply there to increase 
 readability, and because it conceptually makes no sense to compare a class 
 reference to 0.  New programmers shouldn't have to understand the underlying 
 concepts of memory addresses in a language like D, and shouldn't have to 
 know that address 0 means "nothing."  If anything, having "null" makes it 
 easier to learn the language.
 
 You know what I can't stand?  That pesky "if" keyword.  Why can't we just 
 use the ?: operator everywhere?  The fewer keywords, the easier, right?  :P 
 
 

hmm .. # right? :P is that a ternary operator usage? :P // <<-- same here
May 26 2005
prev sibling parent <t t.com> writes:
In article <d75rh5$fg2$1 digitaldaemon.com>, kb3ctd2 yahoo.com says...
 "Sam" <Sam_member pathlink.com> wrote in message 
 news:d75g34$6eb$1 digitaldaemon.com...
 You know what I can't stand?  That pesky "if" keyword.  Why can't we just 
 use the ?: operator everywhere?  The fewer keywords, the easier, right?  :P 

Those who do not know Lisp are condemned to repeat it. :) ( in lisp the 'if' function is 'ternary' (and many other functional languages ( can be used in conjuction with map and reduce ( 'jus sayin' is all ) ) ) ) - Factory
May 27 2005
prev sibling next sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Sam wrote:
 Should I use NULL or 0?
 
 To quote Bjarne Stroustrup, the god of C++:
 
 "In C++, the definition of NULL is 0, so there is only an aesthetic difference.
 I prefer to avoid macros, so I use 0. Another problem with NULL is that people
 sometimes mistakenly believe that it is different from 0 and/or not an integer.
 In pre-standard code, NULL was/is sometimes defined to something unsuitable and
 therefore had/has to be avoided. That's less common these days."
 
 ---->  http://www.research.att.com/~bs/bs_faq2.html#null
 
 So as you can see, even the great Bjarne Stroustrup believes we don't need a
 'null' keyword.
 
 He also believes that garbage collection was never needed in C++, as
 std::auto_ptr<T> can handle everything.
 I don't fully agree with this...  But I can't really say he's wrong here
either.
 
 I guess my question is: What has D taken from this "so called" wisdom from a
"so
 called" guru?
 
 

As Sean Kelly pointed out, your Bjarne Stroustrup thinks it's a good idea to introduce a "null" keyword in C++. except that he's naming it "nullptr", and he's only doing that to avoid name conflicts .. because many people are using "null" already. hmm .. the mistake of no implementing "null" in the first place, caused him to choose a less clean name, "nullptr" ..
May 26 2005
prev sibling parent Dejan Lekic <leka entropy.tmok.com> writes:
With all due respect sir, as a programmer with more than 13 years of working
experience (10 in C/C++ fields), I must say that your "Bjarne-god story" is
not for this newsgroup (i do not want to use some "bad words" actually)...

Think about D as a mix of all goods from all modern languages, and some
original ideas. 
Depending on level of type-safety You want NULL can be treated differently.
I personally like D's approach.

-- 
...........
Dejan Lekic
  http://dejan.lekic.org
  
May 27 2005