View mode: basic / threaded / horizontal-split · Log in · Help
September 15, 2012
Re: References in D
On Saturday, 15 September 2012 at 21:30:03 UTC, Walter Bright 
wrote:
> On 9/15/2012 5:39 AM, Henning Pohl wrote:
>> The way D is dealing with classes reminds me of pointers 
>> because you can null
>> them. C++'s references cannot (of course you can do some nasty 
>> casting).
>
> Doing null references in C++ is simple:
>
> int *p = NULL;
> int& r = *p;
>
> r = 3; // crash

Next time I think before I write.
September 15, 2012
Re: References in D
On 9/15/2012 3:42 PM, Henning Pohl wrote:
> On Saturday, 15 September 2012 at 21:30:03 UTC, Walter Bright wrote:
>> On 9/15/2012 5:39 AM, Henning Pohl wrote:
>>> The way D is dealing with classes reminds me of pointers because you can null
>>> them. C++'s references cannot (of course you can do some nasty casting).
>>
>> Doing null references in C++ is simple:
>>
>> int *p = NULL;
>> int& r = *p;
>>
>> r = 3; // crash
>
> Next time I think before I write.

I wouldn't worry about it. I suspect that most C++ programmers think that 
references cannot be null. C++ is a complex language, and invites assumptions 
about it that are not so.

Const in C++ is another one loaded with incorrect assumptions.
September 15, 2012
Re: References in D
On Saturday, September 15, 2012 16:29:32 Walter Bright wrote:
> I wouldn't worry about it. I suspect that most C++ programmers think that
> references cannot be null. C++ is a complex language, and invites
> assumptions about it that are not so.

It's stuff like that that makes it so that I'll probably never claim that I'm 
an expert in C++ even though I'm probably one of the more knowledgeable about 
it where I work. Even if you think you know it really well, there's always 
_something_ that you miss.

- Jonathan M Davis
September 15, 2012
Re: References in D
On 16-09-2012 01:35, Jonathan M Davis wrote:
> On Saturday, September 15, 2012 16:29:32 Walter Bright wrote:
>> I wouldn't worry about it. I suspect that most C++ programmers think that
>> references cannot be null. C++ is a complex language, and invites
>> assumptions about it that are not so.
>
> It's stuff like that that makes it so that I'll probably never claim that I'm
> an expert in C++ even though I'm probably one of the more knowledgeable about
> it where I work. Even if you think you know it really well, there's always
> _something_ that you miss.
>
> - Jonathan M Davis
>

Now add constant expressions, rvalue references, and move semantics to 
the mix, and understanding C++ suddenly got several orders of magnitude 
harder. ;)

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
September 15, 2012
Re: References in D
On Saturday, September 15, 2012 22:12:57 Timon Gehr wrote:
> In a sane type system, optionality is a concept separate from a
> reference and it is much more general.

I'm not about to dispute that a better type system than we have could be 
devised, but what we have works well for the most part, and in every language 
that I've ever used that has anything like a pointer, pointers can be null. 
And D references are mostly treated like pointers, so it's not only perfectly 
natural for them to be nullable, it would be weird if they couldn't be.

Regardless, we have what we have, and it's not going to change for D2. A type 
system which separated out nullability like you suggest (or like Alex 
suggested with the additon of ? to the type) would be a huge departure from 
what we have.

- Jonathan M Davis
September 15, 2012
Re: References in D
On Sunday, September 16, 2012 01:43:41 Alex Rønne Petersen wrote:
> Now add constant expressions, rvalue references, and move semantics to
> the mix, and understanding C++ suddenly got several orders of magnitude
> harder. ;)

Which reminds me that I really need to spend some time learning C++11...

- Jonathan M Davis
September 16, 2012
Re: References in D
On 9/15/2012 4:35 PM, Jonathan M Davis wrote:
> It's stuff like that that makes it so that I'll probably never claim that I'm
> an expert in C++ even though I'm probably one of the more knowledgeable about
> it where I work. Even if you think you know it really well, there's always
> _something_ that you miss.

Having run C++ validation suites on DMC++, there's plenty that I missed.
September 16, 2012
Re: References in D
On Sat, 15 Sep 2012 20:06:01 +0200, Jonathan M Davis <jmdavisProg@gmx.com>  
wrote:

> On Saturday, September 15, 2012 19:57:03 Henning Pohl wrote:
>> On Saturday, 15 September 2012 at 17:12:23 UTC, Jonathan M Davis
>>
>> wrote:
>> > 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
>>
>> And many usages of null as a state leads to really bad design.
>> There are functions which behaviour is completly different if you
>> pass null instead of a valid pointer/reference. An example would
>> be:
>>
>> http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceIDs.htm
>> l
>
> I'd argue that using null for indicating something other than the lack  
> of a
> value is bad design. But there are plenty of cases where being able to
> indicate that there is no value is useful. And if a function requires  
> that a
> pointer or reference or array or whatever have a value, then there's  
> always
> DbC or exceptions. Just because someone can misuse a feature doesn't  
> mean that
> a feature shouldn't be there.

The want for non-nullable pointers and references does not mean nullable
ones should go - far from it. But using nullable pointers where only non-
null values are meaningful is like a function taking a float and working
only when the value is an integer. We're not saying floats are bad,
we're saying 'if your function only works with integers, then use an int'.

-- 
Simen
September 16, 2012
Re: References in D
On Saturday, 15 September 2012 at 23:34:44 UTC, Jonathan M Davis 
wrote:
> On Saturday, September 15, 2012 16:29:32 Walter Bright wrote:
>> I wouldn't worry about it. I suspect that most C++ programmers 
>> think that
>> references cannot be null. C++ is a complex language, and 
>> invites
>> assumptions about it that are not so.
>
> It's stuff like that that makes it so that I'll probably never 
> claim that I'm
> an expert in C++ even though I'm probably one of the more 
> knowledgeable about
> it where I work. Even if you think you know it really well, 
> there's always
> _something_ that you miss.
>
> - Jonathan M Davis

My favourite C++ wtf:

struct Foo
{
    template <int x, int y> int fun(bool c) { return c ? x : y; }
};

struct Bar
{
    int fun;
};

template <typename T>
int madness()
{
    return T().fun<1, 2>(true);
}

The WTF: madness<Foo> doesn't compile, but madness<Bar> does 
(with a warning).
September 16, 2012
Re: References in D
On 2012-09-15 23:30, Walter Bright wrote:

> Doing null references in C++ is simple:
>
> int *p = NULL;
> int& r = *p;
>
> r = 3; // crash

Won't that crash at the first assignment of "r", since you dereferencing 
a null pointer?.

-- 
/Jacob Carlborg
1 2 3 4 5 6 7 8
Top | Discussion index | About this forum | D home