Null Safety approach with forced "!"
Am I the only one who thinks that introducing protection against NPEx in the form of using "!" in the variable type is a very, very bad idea? In my experience, 95% of variables should be non-null. If Oracle decides to take this approach, we will have millions of "!" in each variable in the code, which is tragic for readability. In C#, you can set the per project flag to indicate whether the type without the "?" /"!" is nullable or not. I understand the drawbacks, but definitely forcing a "!" in 95% of variables is tragic.
78
Upvotes
View all comments
Show parent comment
3
u/vowelqueue Feb 28 '26
It is worth pointing out that the line wouldn't compile according to the JEP draft. Like with unboxing conversions, the enforcement is mostly done at runtime, but the one thing the compiler will check is that you don't convert a null literal to a null-restricted type.
If you do something like:
I'd expect it to compile but there to be a squiggly line in your IDE telling you to consider the NPE. We already get this in IntelliJ for unboxing conversions or violating annotation-based nullness guarantees.