In essence, a Result object is just a container that wraps a success or a failure value for us. Therefore, sometimes you are going to want to get that value out of the container.
As useful as this may seem, we will soon realize that we won't be doing it very often.
Unwrapping Success
The most basic way to retrieve the success value wrapped inside a result is by using . This method will return an optional success value, depending on whether the result was actually successful or not.
We explored various ways to retrieve values from results. Using these methods you can efficiently access the underlying data within a Result object, whether it's a success or a failure.
Similarly, we can use to obtain the failure value held by a Result object.
Unlike , these methods are null-safe. However, in practice, we will not be using them frequently. Especially, since there are more convenient ways to get the success value out of a result.
We can use to provide an alternative success value that must be returned when the result is unsuccessful.
The method is similar to , but it takes a mapping instead of a . The function will receive the failure value to produce the alternative success value.
Finally, we can use and to wrap the value held by an instance of Result into a possibly-empty object.