by kmiroslav · 10y ago · 0 comments
By encoding `null` in its type system, Kotlin lets you manipulate these values directly which leads to code that is much less noisy and just as safe.
The main strength of the first approach is that Option is only one type out of many error-handling structures.
Not every error is handled appropriately by Option/?.
If you have a language like Kotlin where they hard-coded one way of handling errors, it feels very unidiomatic to pick a better fitting error handling type, while in languages where errors are handled by library code, it's a very natural approach.
Which is expected since these two constructs are not aimed at handling errors: they manage missing values.
> If you have a language like Kotlin where they hard-coded one way of handling errors
No, no. `?` is not for handling errors.
Kotlin is as agnostic as Scala for managing errors: you are free to use exceptions, dumb return values or smarter ones (`Either`, `\/`, `Try`, ...).
> Which is expected since these two constructs are not aimed at handling errors: they manage missing values.
Which is a very small part of handling errors in general. As Kotlin offers special syntax for only this case, developers tend to shoehorn many errors into the "missing-value" design to get the "nice" syntax even if a different approach would have been more appropriate.
> Kotlin is as agnostic as Scala for managing errors: you are free to use exceptions, dumb return values or smarter ones (`Either`, `\/`, `Try`, ...).
That's not true in practice:
Just have a look at funktionale: Despite providing almost the same as Scala's error handling types (partially due to the blatant copyright violations) almost nobody uses it. This is a direct result from having a "first-class" construct in the language: It turns library-based designs into second-class citizens.
That's the thing Scala got right, and many of the copy-cat languages got wrong.
Missing values are not errors.
If you look up a key on a map and that key is not present, it's not an error.
> partially due to the blatant copyright violations
Uh copyright what? On an API?!?