Fixing C's Biggest Mistake

Walter Bright newshound2 at digitalmars.com
Sun Jan 1 01:58:18 UTC 2023


On 12/30/2022 11:55 PM, Sebastiaan Koppe wrote:
> On Saturday, 31 December 2022 at 06:34:38 UTC, Walter Bright wrote:
>> Which is better? Both cause the program to quit on a null pointer.
> 
> In a larger program the first one allows the programmer to do the check once and 
> rely on it for the remainder of the program.

Which is what usually happens with nullable pointers. We check once and rely on 
it to be non-null for the rest of the program, and the hardware ensures we 
didn't screw it up.


> Essentially it leverages the type system to make invalid state unrepresentable.

I actually do understand that, I really do. I'm pointing out that the hardware 
makes dereferencing null pointers impossible. Different approach, but with the 
same end result.


> This simplifies subsequent code.

I'm not so sure it does. It requires two types rather than one - one with the 
possibility of a null, one without. Even the pattern matching to convert the 
type is more work than:

    if (p) ...


>> Having a hardware check is perfectly valid for checking things.
> Not all targets have said check though.

True. Some 16 bit processors don't, notably the 8086. The 80286 had it since 
1985 or thereabouts, back in the stone age. My experience with such machines is 
to develop and debug the code on a machine with hardware memory protection, and 
port it to the primitive target as the very last step.

----

I know I'm not convincing anyone, and that's OK. Seg faults are a marvel of 
modern CPU technology, but 99% of programmers regard them as uncool as a zit. D 
will get sumtypes and pattern matching and then everyone can do what works best 
for them. D has always been a language where you can choose between a floor wax 
and a dessert topping.

Personally, I'm most interested in sumtypes and pattern matching as a better 
error handling mechanism than throwing exceptions.


More information about the Digitalmars-d mailing list