ResultᐸValueᐳ - TheRealKoeDev/Railway-Results GitHub Wiki

NameSpace: KoeLib.Patterns.Railway.Results

Example

public static Result<string> GetPageContent(string address)
{
    try
    {
        using (WebClient client = new WebClient())
        {
            return client.DownloadString(address);
        }
    }
    catch
    {
        return Result<string>.Error();
    }
}

public static Result<string> StoreContent(string content, string filename)
{
    try
    {
        DirectoryInfo directory = Directory.CreateDirectory(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "example-downloads")));
        File.WriteAllText(directory.FullName + "\\" + Path.GetFileName(filename), content);
        return "Success";
    }
    catch
    {
        return Result<string>.Error();
    }
}

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

    string status = GetPageContent(address)
    .Bind(content => StoreContent(content, address + ".html"))
    .Match(message => message, () => "Error");

     Console.WriteLine($"{address} download and storage ended with status: {status}");
     Console.ReadLine();
}

Implicit Operators

public static implicit operator Result<TValue>(TValue value);
public static implicit operator Result(Result<TValue> result);

Static Methods

Success

public static Result<TValue> Success(TValue value);

Error

public static Result<TValue> Error();

Instance Methods

OnSuccess

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

OnError

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

FixOnError

public Result<TValue> FixOnError(Func<TValue> onError);

Either

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

Bind

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

BindOnError

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

Ensure

public Result<TValue> Ensure(Func<TValue, bool> condition);

Match

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

KeepOnSuccess

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

KeepOnError

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

KeepEither

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

AsPlainResult

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