kotlin-result: Result 2.0: unsafe unwrapping of `Result.value` and `Result.error`
I noticed that the new inline value class in API 2.0 lets us access result.value directly without checking if it’s actually safe, which wasn’t the case before. Here’s what I mean:
val result: Result<Int, SomeError> = getResult()
result.value // This compiles even if the result is an error, leading to potential runtime errors.
This could lead to runtime exceptions that were harder to make before when the API required type checking and more explicit unwrapping using getOrThrow() (which is a lot easier to catch or denylist through code analysis).
It seems like API 2.0 introduces a downgrade in compile-time safety compared to the previous version. Could we consider reinstating some form of safety, perhaps through Kotlin contracts or another mechanism, to prevent potential runtime errors?
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 8
- Comments: 27 (13 by maintainers)
The
unwrap()API – wellgetOrThrow()in this case – has clear naming and behavior, no issues with that.Setting
getOrThrow()aside, in version 1, you had to check that result wasOkbefore accessingsomeResult.value, ensuring safety. In version 2, you can accessresult.valuewithout any checks, and it’s not obvious that this API is throwing at runtime, even though the code compiles.After upgrading to version 2, we encountered bugs because we accessed
result.valuewithout proper checks. In v1, this was preventable by compiler, in v2 we need to write a custom linter for that. Desired API for us would prevent us from callingresult.valueuntil type is checked - as it was implemented in v1. This is probably the main reason why we want to use something likeResultto begin with. I hope this clarifies things.I do not agree it’s the same though. Semantically it’s very different. With unwrap, it’s clear that it is a separate way to get the value without checking it first, whereas .value is the way to get the successful value both before and after checking.
We didn’t even need to disallow it, no one ever used it and most likely no one ever will. I can almost guarantee it that people will use .value though.
(Sorry for not using proper formatting and probably also not explaining it in the best way… just doing some quick response reaction on the smartphone 😅)
For us at work, in the type of apps I develop, I’d rather have safer compile time access than the optimisations. So this means we’ll keep using version 1 then.
I guess it would be interesting to know how much users of the library fall into the same camp as us.
Also, I’m not sure if version 1 would need to have some support going forward? With updates to Kotlin versions and stuff like that? Probably not new features though.
@michaelbull I should have clarified - in our codebase we disallowed usage of
unwrap()(a common practice in our Rust counterpart too), and instead we relied on compile time safe type checking, before:Now, I can just call
result.valuewithout checking for “type” and risk it throwing an exception at runtime.It being nullable would force it to be boxed at all call sites. See the following example from Kotlin’s docs:
https://kotlinlang.org/docs/inline-classes.html#representation
We simply followed the practice to never call
unwrap()(well except for in tests). We could implement a linter easily but we just didn’t get to that point.