Project Elvis

Nick Treleaven nick at geany.org
Mon Nov 13 15:10:17 UTC 2017


On Saturday, 11 November 2017 at 05:20:39 UTC, codephantom wrote:
> I just saw this about the new 'damnit' operator, for C# 8.
>
> https://github.com/dotnet/csharplang/issues/556

The principle is a good one - by default you cannot dereference 
something that can be null, you get a compiler error instead. If 
you are confident it isn't null, you use a special operator to 
override the compiler check. This is better because:

1. The programmer has to acknowledge that the reference is 
nullable (except where the compiler may be able to prove it is 
not null).
2. People reading the code are informed that potentially the 
reference is null but the programmer thought it wouldn't be, in 
this particular case, documenting the programmer's understanding.
3. Reviewers are freed from checking r's possible assigned value 
in all code paths every time r is dereferenced.

This solution is probably less disruptive to existing code than 
removing null altogether like Rust, which encourages the 
programmer to always handle the not-null and null case for every 
dereference. (Although half the battle is having non-null types).


More information about the Digitalmars-d mailing list