ResultOrErrorᐸErrorᐳ - TheRealKoeDev/Railway-Results GitHub Wiki

NameSpace: KoeLib.Patterns.Railway.Results

Example

public static ResultOrError<string> NetworkIsAvailable()
    => NetworkInterface.GetIsNetworkAvailable() ? ResultOrError<string>.Success() : "No network connection available.";

public static ResultOrError<string> CanPingAddress(string address, int timeout)
{
    using (Ping ping = new Ping())
    {
        try
        {
            bool success = ping.Send(address, timeout).Status == IPStatus.Success;
            return success ? ResultOrError<string>.Success() : "Ping failed.";
        }
        catch(Exception e)
        {
            return e.InnerException == null ? e.Message : e.InnerException.Message;
        }
    }
}

public static void Main()
{
    string address = "www.google.com";

    string message = NetworkIsAvailable()
    .Bind(() => CanPingAddress(address, 1000))
    .Match(() => $"{address} is accessible.", error => error);

    Console.WriteLine(message);
    Console.ReadLine();
}

Implicit Operators

public static implicit operator ResultOrError<TError>(TError error);
public static implicit operator Result(Result<TValue, TError> result);

Static Methods

Create

In seperate class ResultOrError.

public static ResultOrError<TError> Create<TError>(bool success, Func<TError> errorFunc);

Success

public static ResultOrError<TError> Success();

Error

public static ResultOrError<TError> Error(TError error);

Instance Methods

OnSuccess

public ResultOrError<TError> OnSuccess(Action onSuccess);
public Result<TValue, TError> OnSuccess<TValue>(Func<TValue> onSuccess);

OnError

public ResultOrError<TError> OnError(Action<TError> onError);
public ResultOrError<TNewError> OnError<TNewError>(Func<TError, TNewError> onError);

FixOnError

public ResultOrError<TError> OnError(Action<TError> onError);

Either

public ResultOrError<TError> Either(Action onSuccess, Action<TError> onError);
public Result<TValue, TError> Either<TValue>(Func<TValue> onSuccess, Action<TError> onError);
public ResultOrError<TNewError> Either<TNewError>(Action onSuccess, Func<TError, TNewError> onError);
public Result<TValue, TNewError> Either<TValue, TNewError>(Func<TValue> onSuccess, Func<TError, TNewError> onError);

Bind

public ResultOrError<TError> Bind(Func<ResultOrError<TError>> onSuccess);
public Result<TValue, TError> Bind<TValue>(Func<Result<TValue, TError>> onSuccess);
public TResult Bind<TResult>(Func<TResult> onSuccess, Func<TError, TResult> onError) where TResult : IResult;

BindOnError

public Result BindOnError(Func<TError, Result> onError);
public ResultOrError<TNewError> BindOnError<TNewError>(Func<TError, ResultOrError<TNewError>> onError);

Ensure

public ResultOrError<TError> Ensure(Func<bool> condition, Func<TError> error);

Match

public T Match<T>(Func<T> onSuccess, Func<TError, T> onError);

KeepOnSuccess

public ResultOrError<TError> KeepOnSuccess<T>(Func<T> onSuccess, out T kept);

KeepOnError

public ResultOrError<TError> KeepOnError<T>(Func<TError, T> onError, out T kept);

KeepEither

public ResultOrError<TError> KeepEither<T>(Func<T> onSuccess, Func<TError, T> onError, out T keptValue); 
public ResultOrError<TError> KeepEither<T1, T2>(Func<T1> onSuccess, Func<TError, T2> onError, out T1 keptOnSuccess, out T2 keptOnError);

AsPlainResult

public Result AsPlainResult();
⚠️ **GitHub.com Fallback** ⚠️