Handling Errors
Error handling in Rouge works similarly to how the language deals with the absence or presence of values introduced previously. When a function may produce an error, we can wrap the return value of that function into an instance of Result. A Result can either be Ok or an Error, representing that a computation was successful or erroneous, respectively. The Result type and its subtypes can be seen in the following type diagram. As with the chapter on handling the absence or presence of values, the type diagram is intentionally kept concise by omitting fields on subtypes if they are already shown on the parent.
The main difference to the Option type is that Result stores a separate value in case of failure. Result also has two placeholders, T and E which define the types of the Ok and Error cases respectively. An example of how to access both values can be seen in the following snippet. The method called is_ok is used to detect whether the instance of Result is actually an instance of Ok. If that is the case, get_value can be used to obtain the value of the successful case. If the instance of Result turns out to be an instance of Error instead, get_error can be used to obtain the error value.