Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Created June 3, 2020 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominicFinn/684788adc6f1431d663843272f13dcb2 to your computer and use it in GitHub Desktop.
Save DominicFinn/684788adc6f1431d663843272f13dcb2 to your computer and use it in GitHub Desktop.
Results
using System;
using System.Collections.Generic;
namespace ResultThings
{
internal class Result
{
public bool Success { get; }
public string Message { get; }
protected Result(bool success, string message)
{
this.Success = success;
this.Message = message;
}
public static Result Failure(string message)
{
return new Result(false, message);
}
public static Result Successful()
{
return new Result(true, string.Empty);
}
}
internal class Result<T> : Result
{
public T Value { get; }
Result(T value) : base(success: true, string.Empty)
{
this.Value = value;
}
Result(bool success, string message) : base(success, message) {
}
public new static Result<T> Failure(string message)
{
return new Result<T>(false, message);
}
public static Result<T> Successful(T value)
{
return new Result<T>(value);
}
}
internal sealed class Usage
{
public static Result EmptyResult()
{
return Result.Successful();
}
public static Result<string> TypedResult()
{
return Result<string>.Successful("loll");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment