by Nycto · 9y ago · 0 comments
val x: Option[Int] = null someMethod(null)
might be perfectly legitimate or might not, but you can't tell without knowing the details of someMethod (whether it's a Java method or not).That will fail at compile time in Kotlin if the method is written in Kotlin and has a non-nullable parameter. Even if it's Java I think it will fail at compile time if the Java parameter is annotated with @NotNull.
If it is a Java method and not annotated with @NotNull then of course it will be allowed at compile time because how could it possibly not be allowed? What are you saying is preferable in that case?
In idiomatic Scala you would never have a line like that (because you represent absence with optionals rather than null). So you can insta-fail any line like that in code review (or give very careful review in the rare case that it's calling a Java method that accepts a nullable parameter, but good modern Java libraries are moving away from those, so that case should go away).
Scala can only do `Option` since it doesn't have built in support for nullable types.
I fail to see how your point shows any superiority from Scala here, it's the other way around.
At the end of the day, both languages have nullability support but since it's optional in Scala (because library based), Kotlin's approach is superior in that it requires developers to deal with nullability, while Scala developers can ignore it any time they want.
That's not _bad_, but the argument being made by the OP was that nullability in Kotlin was unsafe and inconsistent.
The problems arise when you interop with Java. At that point, shit can be null and you just don't know. My point is that it's the same problem in Scala as it is in Kotlin: Java interop adds a measure of unpredictability that needs to be handled at the touch points.