Result - TheRealKoeDev/Railway-Results GitHub Wiki

NameSpace: KoeLib.Patterns.Railway.Results

Example

public static Result NetworkIsAvailable() => NetworkInterface.GetIsNetworkAvailable();

public static Result CanPingAddress(string address, int timeout)
{
    using (Ping ping = new Ping())
    {
        try
        {
            return ping.Send(address, timeout).Status == IPStatus.Success;
        }
        catch
        {
            return false;
        }
    }
}

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

    string status = NetworkIsAvailable()
    .Bind(() => CanPingAddress(address, 1000))
    .Match(() => "accessible", () => "not accessible");

    Console.WriteLine($"{address} is {status}.");
    Console.ReadLine();
}

Implicit Operators

public static implicit operator Result(bool success);

Static Methods

Create

public static Result Create(bool success);
public static Result<TValue> Create<TValue>(bool success, Func<TValue> valueFunc);
public static Result<TValue, TError> Create<TValue, TError>(bool success, Func<TValue> valueFunc, Func<TError> errorFunc);

Success

public static Result Success();

Error

public static Result Error();

Combine

public static Result Combine(params Result[] results);

Try

public static TryCatchResult<T> Try<T>(Func<T> func);

Instance Methods

OnSuccess

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

OnError

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

FixOnError

public Result FixOnError(Action onError);

Either

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

Bind

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

BindOnError

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

Ensure

public Result Ensure(Func<bool> condition);

Match

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

KeepOnSuccess

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

KeepOnError

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

KeepEither

public Result KeepEither<T>(Func<T> onSuccess, Func<T> onError, out T keptValue); 
public Result KeepEither<T1, T2>(Func<T1> onSuccess, Func<T2> onError, out T1 keptOnSuccess, out T2 keptOnError);
⚠️ **GitHub.com Fallback** ⚠️