base class Result<+T, +E>
Represents the result of a computation that may succeed or fail. Success(T) represents the result of a successful computation and Failure(E) represents the result of a failed computation.
A Result encodes the possibility of failure and requires the developer to explicitly handle the failure case. This can be useful for APIs that can be reasonably expected to fail at runtime, such as when the failure is due to specific runtime values as opposed to a bug in the calling code.
Example:
result = computeSomething();
result match {
| Success(success) -> /* use success */
| Failure(error) -> /* log error and fallback...*/
};
Static Methods
Creating a Result
static fun sourceguard(f: () -> T): Result<T, Exception>Execute the given function, returning as follows:
- If the function completes, returns Success() with the return value
- If it throws, returns Failure() with the exception
Methods
Composition
fun sourcemap<T2>(f: (T) -> T2): Result<T2, E>For Success() values, returns a new result with the results of applying the mapping function to the contained value. Returns Failure values as-is.
For Success() values, returns the result of applying the mapping function to the contained value. Returns Failure values as-is.
Returns the value if thhis is a Success(), otherwise throws.
Returns the error if this is a Failure(), otherwise throws. with the given message.
Transforming to Different Types
fun sourcemaybeSuccess(): Option<T>Returns Some(value) if this is a Success(), otherwise None()
Failure() is less than Success().