Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn on nullable types #1211

Open
symbiogenesis opened this issue Jul 5, 2024 · 1 comment
Open

Turn on nullable types #1211

symbiogenesis opened this issue Jul 5, 2024 · 1 comment

Comments

@symbiogenesis
Copy link
Contributor

symbiogenesis commented Jul 5, 2024

The dreaded NullReferenceException comes from a mistake by the legendary Tony Hoare in 1966, and he has since apologized for it. But most languages have, nevertheless, inherited this mistake.

.NET doesn't provide a real solution at the type level to fully eliminate them, but with the latest .NET versions if you turn on nullable types you can come pretty close to solving this class of problems entirely at compile-time.

And we can do it with confidence that we aren't breaking anything with the following process:

  1. Set <Nullable>enable</Nullable> on a selected project
  2. Put #nullable disable on the top of every code file
  3. Remove that line from the previous step on a file-by-file basis, and modify each file by affixing ? modifiers accordingly.

If enough progress is made we can simply move the <Nullable>enable</Nullable> setting into Directory.Build.props and have it be set for every project in the solution.

I have been looking for a solution that can just automatically add nullability modifiers in a dumb way, which replicates existing nullability guarantees. Haven't found anything.

But, given that it isn't really enforced at the type level, if it compiles then it is going to work as before.

@symbiogenesis
Copy link
Contributor Author

I say that it isn't real because when you turn on nullable types, a variable that is declared without a ? modifier is actually still fully capable of holding a null value, such as when it is a public function and referenced from code that doesn't have nullable types turned on. Because it is all just akin to fancy analyzers, rather than part of the type system.

But far, far, better than nothing. And also, there's yet more analyzers that warn you about unguarded use of non-nullable parameters of public functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant