Class Result<T, E>

Wraps the evaluation of an operation, which could either succeed with (or without) a result or fail with (or without) an error.

This class is always an immutable data structure.

See

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/

Type Parameters

  • T

  • E

Hierarchy

  • Result

Constructors

  • Type Parameters

    • T

    • E

    Parameters

    • result: undefined | T
    • error: undefined | E
    • isSuccess: boolean

    Returns Result<T, E>

Properties

error?: E
isFailure: boolean

True if the Result represents a failure.

Will always be opposite to isSuccess.

See

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/is-failure.html

isSuccess: boolean

True if the Result represents success.

Will always be opposite to isFailure.

See

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/is-failure.html

result?: T

Methods

  • Map's the current Result using onSuccess or onFailure. Returns the result of the evaluation.

    The function passed on onSuccess is used is used to map the current Result if it represents a success. If so, the current value is used as the value parameter.

    The function passed on onFailure is used is used to map the current Result if it represents a failure. If so, the current error is used as the error parameter.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/fold.html

    Type Parameters

    • R

    Parameters

    • args: {
          onFailure: ((error: E) => R);
          onSuccess: ((value: T) => R);
      }
      • onFailure: ((error: E) => R)
          • (error: E): R
          • Parameters

            • error: E

            Returns R

      • onSuccess: ((value: T) => R)
          • (value: T): R
          • Parameters

            • value: T

            Returns R

    Returns R

  • Returns the Result's value if it succeeded. If the result represents a failure, evaluates onFailure with the occurred error as parameter and returns the result of the evaluation.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/get-or-else.html

    Type Parameters

    • R

    Parameters

    • onFailure: ((e: E) => R)
        • (e: E): R
        • Parameters

          • e: E

          Returns R

    Returns T | R

  • Transforms the value of the Result if it represents a success. If not, returns the current result.

    The function on transform is used to transform the value in case the result represents success. If so, the value argument of this callback function will be the current value of the Result. This operation does not mutate the current Result, instead returns a new one with the return of value wrapped.

    If the current Result represents an error, the current result is returned unchanged.

    If the evaluation of the transform function fails, the error will be left uncatched.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/map.html

    Type Parameters

    • R

    Parameters

    • transform: ((value: T) => R)

      The callback to be used to transform the current result.

        • (value: T): R
        • Parameters

          • value: T

          Returns R

    Returns Result<T, E> | Result<R, E>

  • Transforms the value of the Result if it represents a failure. If not, returns the current result.

    The function on transform is used to transform the value in case the result represents success. If so, the value argument of this callback function will be the current value of the Result. This operation does not mutate the current Result, instead returns a new one with the return of value wrapped.

    If the current result represents an error, the current result is returned unchanged.

    If the evaluation of the transform function fails, the Result returned will be a failure with the exception as it's wrapped error.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/map-catching.html

    Type Parameters

    • R

    Parameters

    • transform: ((value: T) => R)

      The callback to be used to transform the current result.

        • (value: T): R
        • Parameters

          • value: T

          Returns R

    Returns Result<T, E> | Result<R, E>

  • Transforms the error of the Result if it represents a failure. If not, returns the current result.

    The function on transform is used to transform the value in case the result represents success. If so, the error argument of this callback function will be the current error of the Result. This operation does not mutate the current Result, instead returns a new one with the return of value wrapped as error.

    If the current Result represents a success, the current result is returned unchanged.

    If the evaluation of the transform function fails, the error will be left uncatched.

    Type Parameters

    • RE

    Parameters

    • transform: ((error: E) => RE)

      The callback to be used to transform the current error.

        • (error: E): RE
        • Parameters

          • error: E

          Returns RE

    Returns Result<T, E> | Result<T, RE>

  • Executes the callback in the action parameter if the current result represents a failure. Always returns the current error unchanged, regardless of the result of the action callback.

    The error parameter of the callback action is the error that the current Result is wrapping.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/on-failure.html

    Parameters

    • action: ((error: E) => void)

      The callback to be executed case the current Result represents a failure.

        • (error: E): void
        • Parameters

          • error: E

          Returns void

    Returns Result<T, E>

  • Executes the callback in the action parameter if the current result represents a succeeded. Always returns the current error unchanged, regardless of the result of the action callback.

    The value parameter of the callback action is the value that the current Result is wrapping.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/on-success.html

    Parameters

    • action: ((value: T) => void)

      The callback to be executed case the current Result represents a succeeded operation.

        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns Result<T, E>

  • Executes the callback transform if the current Result represents a failure and returns it result. Returns the current unchanged Result if it represents a success.

    The error parameter of the transform callback is the error that the current Result is wrapping.

    This function does not catches any errors thrown in the transform callback.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/recover.html

    Type Parameters

    • R

    • NE = E

    Parameters

    • transform: ((error: E) => R)
        • (error: E): R
        • Parameters

          • error: E

          Returns R

    Returns Result<T, E> | Result<R, NE>

  • Executes the callback transform if the current Result represents a failure and returns it result. Returns the current unchanged Result if it represents a success.

    The error parameter of the transform callback is the error that the current Result is wrapping.

    This function catches any errors thrown in the transform callback, and if there are any, the returned Results will be a failure with the error that was thrown wrapped in it.

    See

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/recover-catching.html

    Type Parameters

    • R

    • NE = E

    Parameters

    • transform: ((e: E) => R)
        • (e: E): R
        • Parameters

          • e: E

          Returns R

    Returns Result<T, E> | Result<R, NE>

Generated using TypeDoc