Clay Gillman
05/15/2021, 5:28 AMResult
by default! I was surprised to find this out given how quiet this change was.
Using Result
as a return type for libraries and in general seems like a great middle ground between the looseness of unchecked exceptions and the verboseness of sealed classes as return types.
You can force callers of your method to handle any failure by simply doing fun method(): Result<T> = runCatching { ... }
, allowing uncaught exceptions in the method to bubble up into the failure case of the result. This is much less code than creating a sealed class for every failure case and writing the logic to convert each exception to them. Easier to read and reason about, and doesn't even force you to adopt functional-style-programming.
Now that this is doable with vanilla Kotlin, what does everyone think? Would you adopt this as your default way of error handling? How would you feel consuming a library that exclusively used Result
instead of throwing exceptions?Joost Klitsie
05/15/2021, 11:07 AMHanno
05/15/2021, 11:54 AMClay Gillman
05/15/2021, 3:05 PMJoost Klitsie
05/15/2021, 3:27 PMJoost Klitsie
05/15/2021, 3:29 PM