(non)nullable types

Michel Fortin michel.fortin at michelf.com
Sat Feb 14 06:15:00 PST 2009


On 2009-02-14 08:37:04 -0500, Christopher Wright <dhasenan at gmail.com> said:

> I'm just fine with getting a segfault when using a nullable variable 
> that's null. That's expected behavior.

I leaning to think it'd be best to have non-nullable pointers that just 
throw an exception when you attempt to assign null to them.

That way you can have implicit conversions from nullable to 
non-nullable pointers where the compiler would add a check for null and 
throw; you no longer have to check for null for arguments that 
shouldn't be null (declaring the argument as non-nullable would force 
the caller to make sure it is not null, which cost nothing if the 
caller is passing a non-nullable variable); and you can catch incorrect 
assignment of null to non-nullable pointer before the assignment itself 
(instead of detecting it later it's too late).

Non-nullable should still be the default because it's faster (no check 
for null) and safer to use, and I'd trust the optimiser to remove 
unnecessary checks for null when nullable pointers are casted to 
non-nullable ones (when you check yourself before casting for instance).

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list