Skip to content

Instantly share code, notes, and snippets.

@kekekeks
Created April 11, 2019 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kekekeks/613641448cb912975ddada63dd70b7e0 to your computer and use it in GitHub Desktop.
Save kekekeks/613641448cb912975ddada63dd70b7e0 to your computer and use it in GitHub Desktop.
public class Result<T> : Result
{
public T Value { get; }
public Result(T result)
{
Success = true;
Value = result;
}
public Result()
{
}
public static implicit operator Result<T>(string error)
{
return new Result<T> { Error = error, Success = false };
}
public Result<TRes> Map<TRes>(Func<T, TRes> cb)
{
if (Success)
return Result.Create(cb(Value));
return Error;
}
public bool TryGetValue(out T value)
{
value = Value;
return Success;
}
public bool Get(out T value, out string error)
{
value = Value;
error = Error;
return Success;
}
}
public class Result
{
public static Result<T> Create<T>(T result) => new Result<T>(result);
public bool Success { get; protected set; }
public string Error { get; protected set; }
public Result()
{
Success = true;
}
public static Result Succeeded { get; } = new Result();
public static implicit operator Result(string error)
{
return new Result { Error = error, Success = false };
}
public Result<TRes> Map<TRes>(Func<TRes> cb)
{
if (Success)
return Result.Create(cb());
return Error;
}
public Result<TRes> Map<TRes>(Func<Result<TRes>> cb)
{
if (Success)
return cb();
return Error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment