null references redux + Looney Tunes

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Oct 3 07:54:48 PDT 2009


Jeremie Pelletier wrote:
> Andrei Alexandrescu wrote:
>> Justin Johansson wrote:
>>> For the interest of newsgroups readers, I dropped in at the Cafe the 
>>> other day and
>>> the barista had this to say
>>>
>>> http://cafe.elharo.com/programming/imagine-theres-no-null/
>>>
>>> Disclaimer: YMMV
>>>
>>> Cheers
>>>
>>> -- Justin Johansson
>>
>> This article brings up a very interesting point that beats Walter's 
>> argument to a pulp, then puts salt on it.
>>
>> Walter's overriding argument (I'm sure you know it, he repeated it 
>> claiming nobody understands it until we learned it by heart) was: "I 
>> don't want the compiler to require a value there! People will just put 
>> some crappy value in to get the code to compile, and the code with 
>> errors in it will soldier on instead of duly crashing! How is that 
>> better???" etc.
>>
>> Yet D has structs.
>>
>> Walter knows D has structs, and knows how D structs operate. He put 
>> structs in D because he thought structs, vegetables, exercising, and 
>> flossing are good for you.
> 
> We all know a serving of structs a day makes for healthy programmers!
> 
>> Yet structs operate the exact way that Walter claim is pernicious. 
>> Structs don't have a singular null value and always are nominally 
>> valid objects.
> 
> You're comparing value types to reference types. A class object is also 
> always valid, its the reference that can be null by pointing to no 
> object. You would have the same semantics by using struct pointers.
> 
>> Yet I've never heard Walter continuing his argument with "Just look at 
>> those stinky structs. It must be a million times I had a bug caused by 
>> the absence of null structs! I just had to put a crappy struct there 
>> in my code, and my code soldiered on in error instead of crashing!"
> 
> I never pass structs by value in D, except for returns because of RVO, 
> so I can get a null struct pointer sometimes, but thats what contracts 
> and backtraces are for. If my code was still executing on an invalid 
> struct reference it would be much, much harder to pinpoint the origin of 
> the bug.
> 
>> Why didn't he continue his argument that way? And why didn't anybody 
>> else continue his argument that way? Because nobody has had such a 
>> problem. Everybody uses structs, and everybody's happy about them 
>> lacking null.
> 
> Once again, structs are value types.
> 
>> To complete the irony, Walter and I discussed a while ago about 
>> structs and .init values. We concluded that D, at least for the time 
>> being, will allow struct construction without any code invocation, by 
>> just bitcopying the .init value of the struct over. He was very happy 
>> about that because a lot of code generation got majorly simplified 
>> that way. (I was less happy because that meant less user control over 
>> struct construction.) So Walter was happy that he had for structs a 
>> feature he thinks is amazingly dangerous for classes.
> 
> Classes have an initializer too, it's just copied by the GC after 
> allocation instead.
> 
>> So I don't think Walter's argument is invalid, I think it simply 
>> doesn't exist. This post made it disappear.
>>
>>
>> Andrei
> 
> I disagree, you compared apples to oranges here. Correct me if I'm 
> wrong, but I don't think we should base an argument over reference types 
> by using value types.
> 
> Jeremie

Save for address taking, value types are indistinguishable from 
immutable reference types. Value types don't have a null and that 
doesn't seem to make them unusable. It's a simple point.

Andrei



More information about the Digitalmars-d mailing list