Table of Contents

Class RecoverExtensions

Namespace
Trellis
Assembly
Trellis.Results.dll

Provides extension methods for recovering from failures with a fallback value. Unlike RecoverOnFailureExtensions which requires a function returning a Result, Recover takes a simple fallback value and always produces a success.

public static class RecoverExtensions
Inheritance
RecoverExtensions
Inherited Members

Remarks

This is the complement to ToMaybe. Where ToMaybe says "I don't care about the error, give me Maybe", Recover says "I don't care about the error, use this fallback and stay on the success track."

Methods

Recover<TValue>(Result<TValue>, Func<Error, TValue>)

Recovers from a failure by calling a function that receives the error to produce a fallback value. If the result is a success, returns it unchanged. If the result is a failure, calls the function with the error and returns a success with its return value.

[RailwayTrack(TrackBehavior.Failure)]
public static Result<TValue> Recover<TValue>(this Result<TValue> result, Func<Error, TValue> fallbackFunc)

Parameters

result Result<TValue>

The result to recover from.

fallbackFunc Func<Error, TValue>

The function that receives the error and produces a fallback value.

Returns

Result<TValue>

The original result if success; otherwise a success with the fallback value.

Type Parameters

TValue

Type of the result value.

Recover<TValue>(Result<TValue>, Func<TValue>)

Recovers from a failure by calling a function to produce a fallback value. If the result is a success, returns it unchanged. If the result is a failure, calls the function and returns a success with its return value.

[RailwayTrack(TrackBehavior.Failure)]
public static Result<TValue> Recover<TValue>(this Result<TValue> result, Func<TValue> fallbackFunc)

Parameters

result Result<TValue>

The result to recover from.

fallbackFunc Func<TValue>

The function to produce a fallback value if the result is a failure.

Returns

Result<TValue>

The original result if success; otherwise a success with the fallback value.

Type Parameters

TValue

Type of the result value.

Recover<TValue>(Result<TValue>, TValue)

Recovers from a failure by substituting a fallback value. If the result is a success, returns it unchanged. If the result is a failure, returns a success with the fallback value.

[RailwayTrack(TrackBehavior.Failure)]
public static Result<TValue> Recover<TValue>(this Result<TValue> result, TValue fallback)

Parameters

result Result<TValue>

The result to recover from.

fallback TValue

The fallback value to use if the result is a failure.

Returns

Result<TValue>

The original result if success; otherwise a success with the fallback value.

Type Parameters

TValue

Type of the result value.