[RFC] Throwing an exception with null pointers

Derek Fawcus dfawcus+dlang at employees.org
Sun Apr 13 15:01:23 UTC 2025


On Saturday, 12 April 2025 at 23:11:41 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> ## Language support
>
> As already stated, forced typing is likely to annoy too many 
> people and any pointer typing that could solve that is already 
> out as it is doing the classic managed vs unmanaged pointer 
> typing.
>
> There must be a way for the programmer to acknowledge pointer 
> nullability status without it being required or be part of a 
> type.
> This makes us fairly unique.
>
> Since we cannot require attribution by default, we cannot have 
> a 100% solution with just language level support.
> But we also do not want to have a common error to be in the 
> code and effect runtime if at all possible.
> Or at least in cases where me and Adam Wilson care about 
> regarding eventloops.
>
> Which means we are limited to local only information, similar 
> to C++, except... we can store the results into the type system.
> The result is more code is checked than C++, but also less than 
> say C# by default.
>
> It is possible to opt into more advanced analysis and error 
> when it cannot model your code.
> Which gets us to the same level of support as C# (more or less).

As to language level support for nullable vs non-nullable 
pointers, without having used it yet, I believe I'd like to have 
such.  Picking a default is an issue.

I probably need to play (in C) with the clang __nullable and 
_nonnull markers to see how well they work. From reading the GCC 
docs I can't see benefit from its mechanisms, as they serve to 
guide optimisation rather than checks / assertions at compile 
time and/or checks at runtime.

I think I really want something like Cyclone offered, with forms 
of non-null pointers and  nullable pointers.  Or maybe something 
like Odin/Zig offer with default non-null pointers and optional 
nullable pointers, the latter requring source guards (plus 
dataflow analysis).

As to how that translates to D, I'm not yet sure.

However references alone are not the answer, as I want an 
explicit annotation at function call sites to indicate that a 
pointer/reference may be passed.  Hence I have a quibble with D 
safe mode not allowing passing pointers to locals; only mitigated 
by the 'scoped pointer' annotation when that preview flag is 
enabled.


More information about the Digitalmars-d mailing list