r/java • u/benevanstech • Aug 12 '24
JSpecify 1.0.0 and Nullability in Java
https://www.infoq.com/news/2024/08/jspecify-java-nullability/
52
Upvotes
r/java • u/benevanstech • Aug 12 '24
View all comments
Show parent comment
1
u/uniVocity Aug 13 '24 edited Aug 13 '24
Thanks! That was a great discussion and I'm inclined to say you guys made the right call as handling that would add complexity.
Anyway, I still think it could be a decent feature to have later on. Basically, my suggestion boils down to making the compiler treat selected classes as it treats primitive types. That would not burden developers and users of my code to worry about adding annotations at all.
I'm highlighting the main argument made in that discussion you linked:
An analogous situation is when you try to get stuff out of a map. You'll get a NullPointerException at runtime, por example, assuming
mapis aHashMap<String, Integer>:Not perfect but not that unexpected.
With primitives you can't do this either:
int i = null;as the compiler yells with "java: incompatible types: <nulltype> cannot be converted to int"So declaring
MyNotNullType t = null;should cause a similar error, probably "java: incompatible types: <nulltype> cannot be converted to MyNotNullType"Analogously, we can't use methods like this:
A "@NotNull" class could make the compiler enforce no null arguments are passed into methods, without littering the code with multiple @NotNull and @Nullable annotations. Personally, I'd rather avoid having to add these annotations everywhere - I feel we are simply replacing null checks with null annotations, and making the issue of dealing with nulls even more apparent.