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)

Most upvoted comments

Any update on this issue? We’ve migrated to v2, but this issue is forcing us to downgrade to v1 until it gets a fix.

  1. https://github.com/michaelbull/kotlin-result/issues/104#issuecomment-2557940658
  2. https://github.com/michaelbull/kotlin-result/issues/104#issuecomment-2605025808

This is the exact case same situation as the the unwrap() function: it could be called both before and after a result type check.

The unwrap() API – well getOrThrow() in this case – has clear naming and behavior, no issues with that.

There is no increase or decrease in safety between v1 and v2 in this regard: both of them let you access the underlying value and throw an exception if the Result wasn’t Ok.

Setting getOrThrow() aside, in version 1, you had to check that result was Ok before accessing someResult.value, ensuring safety. In version 2, you can access result.value without 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.value without 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 calling result.value until type is checked - as it was implemented in v1. This is probably the main reason why we want to use something like Result to 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.

As mentioned above, it’s really no different. You always had access to unsafely getting the underlying value (by calling .unwrap()), you just don’t do it with a function call now.

@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:

val result: Result<Int> = ...
// result.value is not possible to call until type is checked - good!:
when (result) {
  is Ok -> result.value
  is Err -> ...

Now, I can just call result.value without 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:

interface I

@JvmInline
value class Foo(val i: Int) : I

fun asNullable(i: Foo?) {}

fun main() {
    val f = Foo(42)

    asNullable(f)  // boxed: used as Foo?, which is different from Foo
}

https://kotlinlang.org/docs/inline-classes.html#representation

How did you disallow it?

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.

Can you disallow calling value the same way? Before value could be called on type casted Ok value. Now, value can be called in different contexts: before and after a result type check - so it doesn’t really make sense to disallow .value usage entirely. We would need to implement a much more sophisticated lint check.