NonNull template

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Fri Apr 18 02:48:59 UTC 2025


On 18/04/2025 2:35 PM, Walter Bright wrote:
>     It works, but the syntax/defaults is backwards. Why does the unusual
>     case of a nullable pointer get the nice syntax while the common case
>     gets the |NonNull!(int*)| syntax? Who is going to write that all
>     over their code?
> 
> Backwards compatibility. The NonNull is the addition, the nullable is 
> the existing. Changing the existing behavior would be a massive disruption.

D is designed around the type state initialized, aka nullable.

This was a _very_ smart thing to do, before type state analysis was ever 
mainstream. Walter this is by far one of the best design decisions you 
have ever made.

Trying to change the default type state for pointers to non-null would 
be absolutely horrific.

Its possible to prove a variable is non-null, but if we start painting 
pointers themselves in the type system? Ughhhhhh, the pain application 
VM languages are having over this isn't worth it, in the context of D. 
We can do a lot better than that.

If this doesn't work without annotation (in a single compilation run), 
we've failed.

```d
void main() {
	func(new int); // ok
	func(null); // error
}

void func(int* ptr) {
	int v = *ptr;
}
```



More information about the Digitalmars-d mailing list