Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): update kotlinresult to v2 (major) #315

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

renovate[bot]
Copy link
Contributor

@renovate renovate bot commented Mar 17, 2024

This PR contains the following updates:

Package Change Age Adoption Passing Confidence
com.michael-bull.kotlin-result:kotlin-result-coroutines 1.1.21 -> 2.0.0 age adoption passing confidence
com.michael-bull.kotlin-result:kotlin-result 1.1.21 -> 2.0.0 age adoption passing confidence

Release Notes

michaelbull/kotlin-result (com.michael-bull.kotlin-result:kotlin-result-coroutines)

v2.0.0

Compare Source

  • The Result type is now an inline value class for reduced runtime overhead (981fbe2)
    • Before & After comparisons outlined below
    • Also see the Overhead design doc on the wiki
  • Previously deprecated behaviours have been removed (eecd1b7)

Migration Guide

Ok/Err as Types

The migration to an inline value class means that using Ok/Err as types is no longer valid.

Consumers that need to introspect the type of Result should instead use Result.isOk/Result.isErr booleans. This naming scheme matches Rust's is_ok & is_err functions.

Before:

public inline fun <V, E, U> Result<V, E>.mapOrElse(default: (E) -> U, transform: (V) -> U): U {
    return when (this) {
        is Ok -> transform(value)
        is Err -> default(error)
    }
}

After:

public inline fun <V, E, U> Result<V, E>.mapOrElse(default: (E) -> U, transform: (V) -> U): U {
    return when {
        isOk -> transform(value)
        else -> default(error)
    }
}
Type Casting

When changing the return type to another result, e.g. the map function which goes from Result<V, E> to Result<U, E>, consumers are encouraged to use the asOk/asErr extension functions in conjunction with the isOk/isErr guard.

The example below calls asErr which unsafely casts the Result<V, E to Result<Nothing, E>, which is acceptable given the isOk check, which satisfies the Result<U, E> return type.

The asOk/asOk functions should not be used outside of a manual type guard via isOk/isErr - the cast is unsafe.

public inline infix fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> {
    return when {
        isOk -> Ok(transform(value))
        else -> this.asErr() // unsafely typecasts Result<V, E> to Result<Nothing, E>
    }
}
Removal of Deprecations

The following previously deprecated behaviours have been removed in v2.

  • binding & SuspendableResultBinding, use coroutineBinding instead
  • and without lambda argument, use andThen instead
  • ResultBinding, use BindingScope instead
  • getOr without lambda argument, use getOrElse instead
  • getErrorOr without lambda argument, use getErrorOrElse instead
  • getAll, use filterValues instead
  • getAllErrors, use filterErrors instead
  • or without lambda argument, use orElse instead
  • Result.of, use runCatching instead
  • expect with non-lazy evaluation of message
  • expectError with non-lazy evaluation of message

Inline Value Class - Before & After

The base Result class is now modelled as an inline value class. References to Ok<V>/Err<E> as types should be replaced with Result<V, Nothing> and Result<Nothing, E> respectively.

Calls to Ok and Err still function, but they no longer create a new instance of the Ok/Err objects - instead these are top-level functions that return a type of Result. This change achieves code that produces zero object allocations when on the "happy path", i.e. anything that returns an Ok(value). Previously, every successful operation wrapped its returned value in a new Ok(value) object.

The Err(error) function still allocates a new object each call by internally wrapping the provided error with a new instance of a Failure object. This Failure class is an internal implementation detail and not exposed to consumers. As a call to Err is usually a terminal state, occurring at the end of a chain, the allocation of a new object is unlikely to cause a lot of GC pressure unless a function that produces an Err is called in a tight loop.

Below is a comparison of the bytecode decompiled to Java produced before and after this change. The total number of possible object allocations is reduced from 4 to 1, with 0 occurring on the happy path and 1 occurring on the unhappy path.

Before: 4 object allocations, 3 on happy path & 1 on unhappy path

public final class Before {
    @&#8203;NotNull
    public static final Before INSTANCE = new Before();

    private Before() {
    }

    @&#8203;NotNull
    public final Result<Integer, ErrorOne> one() {
        return (Result)(new Ok(50));
    }

    public final int two() {
        return 100;
    }

    @&#8203;NotNull
    public final Result<Integer, ErrorThree> three(int var1) {
        return (Result)(new Ok(var1 + 25));
    }

    public final void example() {
        Result $this$map$iv = this.one(); // object allocation (1)
        Result var10000;
        if ($this$map$iv instanceof Ok) {
            Integer var10 = INSTANCE.two();
            var10000 = (Result)(new Ok(var10)); // object allocation (2)
        } else {
            if (!($this$map$iv instanceof Err)) {
                throw new NoWhenBranchMatchedException();
            }

            var10000 = $this$map$iv;
        }

        Result $this$mapError$iv = var10000;
        if ($this$mapError$iv instanceof Ok) {
            var10000 = $this$mapError$iv;
        } else {
            if (!($this$mapError$iv instanceof Err)) {
                throw new NoWhenBranchMatchedException();
            }

            ErrorTwo var11 = ErrorTwo.INSTANCE;
            var10000 = (Result)(new Err(var11)); // object allocation (3)
        }

        Result $this$andThen$iv = var10000;
        if ($this$andThen$iv instanceof Ok) {
            int p0 = ((Number)((Ok)$this$andThen$iv).getValue()).intValue();
            var10000 = this.three(p0); // object allocation (4)
        } else {
            if (!($this$andThen$iv instanceof Err)) {
                throw new NoWhenBranchMatchedException();
            }

            var10000 = $this$andThen$iv;
        }

        String result = var10000.toString();
        System.out.println(result);
    }

    public static abstract class Result<V, E> {
        private Result() {
        }
    }

    public static final class Ok<V> extends Result {
        private final V value;

        public Ok(V value) {
            this.value = value;
        }

        public final V getValue() {
            return this.value;
        }

        public boolean equals(@&#8203;Nullable Object other) {
            if (this == other) {
                return true;
            } else if (other != null && this.getClass() == other.getClass()) {
                Ok var10000 = (Ok)other;
                return Intrinsics.areEqual(this.value, ((Ok)other).value);
            } else {
                return false;
            }
        }

        public int hashCode() {
            Object var10000 = this.value;
            return var10000 != null ? var10000.hashCode() : 0;
        }

        @&#8203;NotNull
        public String toString() {
            return "Ok(" + this.value + ')';
        }
    }
    
    public static final class Err<E> extends Result {
        private final E error;

        public Err(E error) {
            this.error = error;
        }

        public final E getError() {
            return this.error;
        }

        public boolean equals(@&#8203;Nullable Object other) {
            if (this == other) {
                return true;
            } else if (other != null && this.getClass() == other.getClass()) {
                Before$Err var10000 = (Err)other;
                return Intrinsics.areEqual(this.error, ((Err)other).error);
            } else {
                return false;
            }
        }

        public int hashCode() {
            Object var10000 = this.error;
            return var10000 != null ? var10000.hashCode() : 0;
        }

        @&#8203;NotNull
        public String toString() {
            return "Err(" + this.error + ')';
        }
    }
}

After: 1 object allocation, 0 on happy path & 1 on unhappy path

public final class After {
    @&#8203;NotNull
    public static final After INSTANCE = new After();

    private After() {
    }

    @&#8203;NotNull
    public final Object one() {
        return this.Ok(50);
    }

    public final int two() {
        return 100;
    }

    @&#8203;NotNull
    public final Object three(int var1) {
        return this.Ok(var1 + 25);
    }

    public final void example() {
        Object $this$map_u2dj2AeeQ8$iv = this.one();
        Object var10000;
        if (Result.isOk_impl($this$map_u2dj2AeeQ8$iv)) {
            var10000 = this.Ok(INSTANCE.two());
        } else {
            var10000 = $this$map_u2dj2AeeQ8$iv;
        }

        Object $this$mapError_u2dj2AeeQ8$iv = var10000;
        if (Result.isErr_impl($this$mapError_u2dj2AeeQ8$iv)) {
            var10000 = this.Err(ErrorTwo.INSTANCE); // object allocation (1)
        } else {
            var10000 = $this$mapError_u2dj2AeeQ8$iv;
        }

        Object $this$andThen_u2dj2AeeQ8$iv = var10000;
        if (Result.isOk_impl($this$andThen_u2dj2AeeQ8$iv)) {
            int p0 = ((Number) Result.getValue_impl($this$andThen_u2dj2AeeQ8$iv)).intValue();
            var10000 = this.three(p0);
        } else {
            var10000 = $this$andThen_u2dj2AeeQ8$iv;
        }

        String result = Result.toString_impl(var10000);
        System.out.println(result);
    }

    @&#8203;NotNull
    public final <V> Object Ok(V value) {
        return Result.constructor_impl(value);
    }

    @&#8203;NotNull
    public final <E> Object Err(E error) {
        return Result.constructor_impl(new Failure(error));
    }

    public static final class Result<V, E> {
        @&#8203;Nullable
        private final Object inlineValue;

        public static final V getValue_impl(Object arg0) {
            return arg0;
        }

        public static final E getError_impl(Object arg0) {
            Intrinsics.checkNotNull(arg0, "null cannot be cast to non-null type Failure<E of Result>");
            return ((Failure) arg0).getError();
        }

        public static final boolean isOk_impl(Object arg0) {
            return !(arg0 instanceof Failure);
        }

        public static final boolean isErr_impl(Object arg0) {
            return arg0 instanceof Failure;
        }

        @&#8203;NotNull
        public static String toString_impl(Object arg0) {
            return isOk_impl(arg0) ? "Ok(" + getValue_impl(arg0) + ')' : "Err(" + getError_impl(arg0) + ')';
        }

        @&#8203;NotNull
        public String toString() {
            return toString_impl(this.inlineValue);
        }

        public static int hashCode_impl(Object arg0) {
            return arg0 == null ? 0 : arg0.hashCode();
        }

        public int hashCode() {
            return hashCode_impl(this.inlineValue);
        }

        public static boolean equals_impl(Object arg0, Object other) {
            if (!(other instanceof Result)) {
                return false;
            } else {
                return Intrinsics.areEqual(arg0, ((Result) other).unbox_impl());
            }
        }

        public boolean equals(Object other) {
            return equals_impl(this.inlineValue, other);
        }

        private Result(Object inlineValue) {
            this.inlineValue = inlineValue;
        }

        @&#8203;NotNull
        public static <V, E> Object constructor_impl(@&#8203;Nullable Object inlineValue) {
            return inlineValue;
        }

        public static final Result box_impl(Object v) {
            return new Result(v);
        }

        public final Object unbox_impl() {
            return this.inlineValue;
        }

        public static final boolean equals_impl0(Object p1, Object p2) {
            return Intrinsics.areEqual(p1, p2);
        }
    }

    static final class Failure<E> {
        private final E error;

        public Failure(E error) {
            this.error = error;
        }

        public final E getError() {
            return this.error;
        }

        public boolean equals(@&#8203;Nullable Object other) {
            return other instanceof Failure && Intrinsics.areEqual(this.error, ((Failure)other).error);
        }

        public int hashCode() {
            Object var10000 = this.error;
            return var10000 != null ? var10000.hashCode() : 0;
        }

        @&#8203;NotNull
        public String toString() {
            return "Failure(" + this.error + ')';
        }
    }
}


Configuration

📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

Rebasing: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 Ignore: Close this PR and you won't be reminded about these updates again.


  • If you want to rebase/retry this PR, check this box

This PR was generated by Mend Renovate. View the repository job log.

@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 2 times, most recently from d7f02e8 to 9a016e6 Compare March 19, 2024 00:07
@grodin
Copy link
Collaborator

grodin commented Mar 19, 2024

Not at all sure I want to merge this. There's a significant decrease in type safety in v2 for a decrease in allocations which I'm not convinced is the right tradeoff.

@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 6 times, most recently from 9cae90b to 06202e2 Compare March 26, 2024 08:15
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 5 times, most recently from 65434ad to e7f0972 Compare April 5, 2024 11:37
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 4 times, most recently from 109815c to 45f26fe Compare April 17, 2024 17:57
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 2 times, most recently from defc30c to 581560d Compare April 18, 2024 08:14
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 7 times, most recently from 0273b85 to 2049e8c Compare May 7, 2024 13:59
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 3 times, most recently from 110af4e to 719b640 Compare May 17, 2024 18:35
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 3 times, most recently from 0b27a42 to 6f75e0c Compare July 2, 2024 09:41
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 3 times, most recently from c7df2cb to 2d60807 Compare July 17, 2024 07:13
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 5 times, most recently from 4508ed9 to 0d94fc3 Compare July 28, 2024 09:13
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 2 times, most recently from be9ec7a to cf5e821 Compare August 15, 2024 11:35
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 2 times, most recently from 7aa6184 to 4c17fda Compare August 20, 2024 07:19
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 4 times, most recently from 53dcf10 to ebf2934 Compare September 3, 2024 16:19
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 4 times, most recently from cf37591 to 7844e13 Compare September 12, 2024 08:38
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch 3 times, most recently from a232c8b to 5a3bfcf Compare September 17, 2024 07:21
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch from 5a3bfcf to bcd2115 Compare October 4, 2024 14:53
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch from bcd2115 to f6a771e Compare October 13, 2024 09:12
@renovate renovate bot force-pushed the renovate/major-kotlinresult branch from f6a771e to 4e42e1c Compare October 13, 2024 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant