|
|
|
|
|
by lmm
3383 days ago
|
|
> First of all, Scala's Option is a monad, no such thing with Kotlin's nullable type. Is your point that Kotlin's type violates the monad laws? It does, but all that means is that certain constructs behave in really surprising ways and refactors that look obviously safe aren't. I don't see how that could ever be construed as an advantage. If Kotlin's type conformed to the laws (which would be a great improvement) then it would be a monad even if you ignored that fact. All ignoring it means is that you don't get to take advantage of some common generic functions, but have to write them out separately every time instead. > And second, Kotlin's nullability is enforced by the compiler so you can't escape it. In Scala, you are welcome to ignore nullable values whenever it pleases you (or when you forget to wrap them in an Option). In Scala you're forced to check Options (or, better, handle both cases), so it's the same. Yes, if a "bare" null were somehow to get into your program then you would miss it. But where would that null come from? Only from a Java library and that's exactly the same problem in Kotlin. (Theoretically you could also get one coming out of some really bad Scala code, but the library ecosystem knows better than that, and in your own code you either simply don't do that or you use wartremover to enforce that you never do that) |
|
But you're never forced to use them. That's the main problem with Scala's approach: it's entirely er... optional.
The Scala compiler will never tell you "You can't write a.foo() because a may be null".
> Only from a Java library and that's exactly the same problem in Kotlin
No, it's not. The Kotlin compiler will not let such values enter Kotlin code until you've told it precisely whether that type is nullable or not.
Scala has no such mechanism so null values can enter Scala code without the compiler having any say over it.