September 18, 2012
On Tue, 18 Sep 2012 16:56:31 +0200, Mehrdad <wfunction@hotmail.com> wrote:

> On Saturday, 15 September 2012 at 23:28:36 UTC, Walter Bright wrote:
>> I wouldn't worry about it. I suspect that most C++ programmers think that references cannot be null.
>
>
> Yeah, they can't be null _legally_.
>
> Kind of like how in D you can't strip away const _legally_.
>
> But the compiler doesn't complain if you don't obey the rules.

Well, D at least makes you jump through hoops to strip away const,
while C++ assumes that however stupid the thing you seem to be
doing is, you probably intended to be that stupid.

-- 
Simen
September 24, 2012
On Sun, 16 Sep 2012 23:46:34 +0100, deadalnix <deadalnix@gmail.com> wrote:

> Le 15/09/2012 19:13, Jonathan M Davis a écrit :
>> On Saturday, September 15, 2012 15:24:27 Henning Pohl wrote:
>>> On Saturday, 15 September 2012 at 12:49:23 UTC, Russel Winder
>>>
>>> wrote:
>>>> On Sat, 2012-09-15 at 14:44 +0200, Alex Rønne Petersen wrote:
>>>> […]
>>>>
>>>>> Anyway, it's too late to change it now.
>>>>
>>>> I disagree. There are always opportunities to make changes to
>>>> things,
>>>> you just have manage things carefully.
>>>
>>> I don't know if people really use the ability of references being
>>> null. If so, large amounts of code will be broken.
>>
>> Of course people use it. Having nullable types is _highly_ useful. It would
>> suck if references were non-nullable. That would be _horrible_ IMHO. Having a
>> means to have non-nullable references for cases where that makes sense isn't
>> necessarily a bad thing, but null is a very useful construct, and I'd _hate_
>> to see normal class references be non-nullable.
>>
>> - Jonathan M Davis
>
> Years of java have proven me the exact opposite. Nullable is a usefull construct, but nullable by default is on the wrong side of the force.

I think it depends on your background.  Most of my experience has been with C and C++ and I agree with Jonathan that null is incredibly useful, and something I use a lot.  In fact, I am often annoyed that 'int' doesn't have an equivalent value, and instead I have to invent a magic number and ensure it's never a possible valid value.

What I've noticed looking at Java code written by others is that null as a possible state is ignored by the vast bulk of the code, which is completely the opposite when reviewing C/C++ code where null is checked for and handled where applicable.  I think it's a mindset thing brought on by the language, or how it's taught, or something.  It seems to me that Java would have benefited from non-null references :p

My uses of null all seem to boil down to being able to represent something as being "not there yet" or "not specified" etc without having to resort to a magic value, or 2nd flag/boolean/parameter.  I find null is a nice clean way to do this.

As to whether it should be the default or not.. well, you might have a point there.  I think I probably want to use null less than otherwise.

But, all we need is a compiler which says "Oi, you haven't initialised this" forcing me to explicitly set it to null where desired, or a valid value and problem solved, right?  That combined with a construct like NotNull!(T) which would assert in debug that the reference is not null and you can basically stop doing null checks in release code.  Win win, right?

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
September 24, 2012
Regan Heath:

> In fact, I am often annoyed that 'int' doesn't have an equivalent value, and instead I have to invent a magic number and ensure it's never a possible valid value.

Try to start using Nullable of Phobos:

http://dlang.org/phobos/std_typecons.html#Nullable

Bye,
bearophile
September 24, 2012
On Mon, 24 Sep 2012 13:30:29 +0100, bearophile <bearophileHUGS@lycos.com> wrote:

> Regan Heath:
>
>> In fact, I am often annoyed that 'int' doesn't have an equivalent value, and instead I have to invent a magic number and ensure it's never a possible valid value.
>
> Try to start using Nullable of Phobos:
>
> http://dlang.org/phobos/std_typecons.html#Nullable

In C? :p

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
September 24, 2012
On Mon, 24 Sep 2012 14:38:56 +0200, Regan Heath <regan@netmail.co.nz> wrote:

> On Mon, 24 Sep 2012 13:30:29 +0100, bearophile <bearophileHUGS@lycos.com> wrote:
>
>> Regan Heath:
>>
>>> In fact, I am often annoyed that 'int' doesn't have an equivalent value, and instead I have to invent a magic number and ensure it's never a possible valid value.
>>
>> Try to start using Nullable of Phobos:
>>
>> http://dlang.org/phobos/std_typecons.html#Nullable
>
> In C? :p

Worth a try. :p


-- 
Simen
September 24, 2012
On 9/24/12, bearophile <bearophileHUGS@lycos.com> wrote:
> Try to start using Nullable of Phobos: http://dlang.org/phobos/std_typecons.html#Nullable

It could be a little more usable if it was (pseudocode untested):

struct Nullable(P)
{
    P payload;
    bool _isNull = true;

    void opAssign(P p)
    {
        payload = p;
        _isNull = false;
    }

    bool opEquals(typeof(null) t)
    {
        return _isNull;
    }

    bool opEquals(P p)
    {
        return !_isNull && payload == p;
    }
}

void main()
{
    Nullable!int a;
    assert(a == null);
    a = 5;
    assert(a != null);
    assert(a == 5);
}

Unfortunately (or fortunately) you can't implement an 'is' operator overload.
September 25, 2012
On Tuesday, 18 September 2012 at 19:30:24 UTC, Simen Kjaeraas wrote:
> On Tue, 18 Sep 2012 16:56:31 +0200, Mehrdad <wfunction@hotmail.com> wrote:
>
>> On Saturday, 15 September 2012 at 23:28:36 UTC, Walter Bright wrote:
>>> I wouldn't worry about it. I suspect that most C++ programmers think that references cannot be null.
>>
>>
>> Yeah, they can't be null _legally_.
>>
>> Kind of like how in D you can't strip away const _legally_.
>>
>> But the compiler doesn't complain if you don't obey the rules.
>
> Well, D at least makes you jump through hoops to strip away const,


What does this have to do with const?



> while C++ assumes that however stupid the thing you seem to be doing is, you probably intended to be that stupid.


Uhm, pardon? Not only is it irrelevant, it's also wrong:
It's harder to do that in C++ than in D.

In C++ you need const_cast (you shouldn't be using a C-style casts at all).

In D all you have is cast(), and it's damn easy to strip away const, especially when dealing with templates. And the compiler doesn't complain either!
September 25, 2012
On Tuesday, 25 September 2012 at 09:00:39 UTC, Mehrdad wrote:
> What does this have to do with const?
> ...
> Not only is it irrelevant, it's also wrong:
> It's harder to do that in C++ than in D.


My bad, it's not irrelevant, I misunderstood your reasoning.

It's still wrong though. :)
October 03, 2012
On Mon, Sep 24, 2012 at 5:23 AM, Regan Heath <regan@netmail.co.nz> wrote:

> What I've noticed looking at Java code written by others is that null as a possible state is ignored by the vast bulk of the code, which is completely the opposite when reviewing C/C++ code where null is checked for and handled where applicable.  I think it's a mindset thing brought on by the language, or how it's taught, or something.  It seems to me that Java would have benefited from non-null references :p
>
>
Just because (from your experience) null checks are more prevalent in C or C++ compared to Java does not mean that any of those languages could not benefit from non-nullable types. The entire point is to statically prove that a certain reference/pointer cannot be null. You are better able to reason about the code, and it is done at compile time with no run time overhead. Furthermore, you avoid the possibility of forgetting to put in the checks in the first place.

--
Ziad


October 03, 2012
On Saturday, 15 September 2012 at 17:12:23 UTC, Jonathan M Davis wrote:
> On Saturday, September 15, 2012 15:24:27 Henning Pohl wrote:
>> On Saturday, 15 September 2012 at 12:49:23 UTC, Russel Winder
>> 
>> wrote:
>> > On Sat, 2012-09-15 at 14:44 +0200, Alex Rønne Petersen wrote:
>> > […]
>> > 
>> >> Anyway, it's too late to change it now.
>> > 
>> > I disagree. There are always opportunities to make changes to
>> > things,
>> > you just have manage things carefully.
>> 
>> I don't know if people really use the ability of references being
>> null. If so, large amounts of code will be broken.
>
> Of course people use it. Having nullable types is _highly_ useful. It would
> suck if references were non-nullable. That would be _horrible_ IMHO. Having a
> means to have non-nullable references for cases where that makes sense isn't
> necessarily a bad thing, but null is a very useful construct, and I'd _hate_
> to see normal class references be non-nullable.
>
> - Jonathan M Davis

Agreed. Nullable types are a feature not a bug. There is no need to change it. Bugs occur when you do not know the language rules and make assumptions instead. This can happen whith any language and any rules. As to an example use of nullable references: consider a board game (for example chess). The boards has cells which can be empty or occupied. Model this with an array of class objects  representing pieces. null reference means a cell is not occupied. If you want to remove a piece from the board assign null to it and GC will take care of the rest. Now, doing this with full objects representing empty cells would require needless work to define such "null" objects and would be wasteful of memory (typically boards are sparsely populated). Now imagine a really big board and every cell holding references to useless objects simulating null references. It would not make sense. Saying that null references are evil is just propaganda. Let's keep D a sane language.