Proposal: Cast null to non-nullable type should (optionally) throw exception #3663
Unanswered
betty-crokker
asked this question in
Language Ideas
Replies: 1 comment
-
See #3615 (comment), which is being considered orthogonally to NRTs. For now at least, NRTs intentionally don't impact the behavior of the source. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
NRT is a great step forward, but it's time to start thinking about the next step in preventing null pointer exceptions.
Right now we can set Nullable to Disable, Enable, Warnings, or Annotations. Please add a 5th level which is "Exception". When set, the compiler ensures that non-nullable values really, really, aren't ever set to null.
Code like this:
gives a compiler warning now which is good, but there are many situations where the assignment of a null to a variable isn't as obvious, and throwing an exception at the point where the problem occurs is always preferable to throwing an exception much later when the null pointer is dereferenced.
I understand that one of the design goals of the NRT team was to have zero impact on the semantic behavior of existing code; my proposed change does not conflict with this design goal, as existing code will not have this setting and thus will not change.
Beta Was this translation helpful? Give feedback.
All reactions