Java return values not-null by default


#1

I already wrote about 2kloc of Kotlin code in mixed Java/Scala/Kotlin environment. This sugestion is based on practical experience with Kotlin code.

I have concern about the way null-safety affect  interaction with existing Java code. I feel return values from Java code  should be non-null by default. And compiler should add ‘!!’ on each Java call, to stop NPE sneaking into Kotlin code.

***

Right now  “java-nullable-by-default” feels bit like the Checked Exceptions in  Java. It forces you to pay price for (maybe unwanted)  benefit. “Java-nullable-by-default” does not provide  aditional security. It forces people to use sure (!!) operator and teaches them to ignore nulls.  I feel existing projects should be converted to null-safe code incrementaly, and it is hard with habit “oh just another null-able to ignore”.

It can also lead  to incorrect code. Consider code bellow. I know '?' operator is  questionable. But it is common in Groovy and other languages, sure '!!' operator is nevelty.   Nullable-by-default may actually force people to use '?' on  massive scale.

    val out = JavaCode.openStream();
    out?.print("something");

***

Other  point is current direction of null-safety in Java.  Some tools provide  annotation based null-safety warnings (Idea, Eclipse, Findbugs). Some  libraries already use @Nullable annotations (Guava for example). In  future @Nullable annotation will be part of  Java Standard Library.

Usually people avoid using @NotNull for verbosity.  Instead they assumme code is null-safe and only annotate exceptions with  @Nullable. Guava is huge example of this. If this approach will become mainstream,  it would isolate Kotlin and made it very allien.

***

External annotations are not solution for Java-nullable-by-default  problem. There are gigabytes of existing code. It is simply not possible  to annotate all of it.

"Java-nullable-by-default" increases learning and adoption curve. It feels like Scala type system, it hits you into face after typing just a few lines in Kotlin editor. Even my simple-hobby app uses 20MB of field specific Java libraries, which I will have to annotate myself. Imagine large corporation choosing between Kotlin, Scala or Ceylon.

Also 'Java-nullable-by-default' goes agaist principle of  annotations (metadata). Default values should be set sensibly. We should  not have to annotate every single method in universe. Only exceptions  such as 'Map.get()' should be annotated.

#2

We'll first see how much automated annotation help and then consider changing the defaults.


#3

I am not sure this is valid test scenario. You will be testing mostly with Java Core libraries, which will be annotated very quickly.

And I just reliased another problem: External annotation tools (infer nullability etc) will have to be ported to Eclipse as well.