“The primary use cases are expected to be variables, parameters and return values, but they can be used anywhere that tracking nullness makes conceptual sense.”
I’d really love to be able to annotate a class as @NotNull, or implement some NotNull interface that won’t allow my code to compile if any instance of this class or subclasses could be passed around as null.
This would reduce the need to litter method parameters with @NotNull, or to add the suggested ? or unless necessary (when classes aren’t declared as notNull)
It's actually very rare to need to write `@NonNull`. We almost didn't even include it in the jar! Just put `@NullMarked` on your class/package/module instead.
Are there any examples available that show how to specify @NullMarked at the package or module level? I've combed through the JSpecify docs, but haven't found any.
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:
I don't think there is any such thing as a class that should viewed as "never let this be nullable". As soon as you try to get that thing out of a Map, there you are.
An analogous situation is when you try to get stuff out of a map. You'll get a NullPointerException at runtime, por example, assuming map is a HashMap<String, Integer>:
int i = map.get("nothing"); //NullPointer exception
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:
public void execute(){
//compilation error - java: incompatible types:
//<nulltype> cannot be converted to int
doSomething(null);
}
public void doSomething(int v){
//do stuff
}
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.
5
u/uniVocity Aug 12 '24
“The primary use cases are expected to be variables, parameters and return values, but they can be used anywhere that tracking nullness makes conceptual sense.”
I’d really love to be able to annotate a class as @NotNull, or implement some NotNull interface that won’t allow my code to compile if any instance of this class or subclasses could be passed around as null.
This would reduce the need to litter method parameters with @NotNull, or to add the suggested ? or unless necessary (when classes aren’t declared as notNull)