Created
June 3, 2020 14:51
-
-
Save DominicFinn/684788adc6f1431d663843272f13dcb2 to your computer and use it in GitHub Desktop.
Results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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