Result API Reference¶
Overview¶
The Result<T>
class is the core type in TinyResult that represents the outcome of an operation that can either succeed or fail. It provides a type-safe way to handle success and failure cases in your applications.
Properties¶
IsSuccess¶
public bool IsSuccess { get; }
IsFailure¶
public bool IsFailure { get; }
Value¶
public T? Value { get; }
Error¶
public Error? Error { get; }
Static Methods¶
Success¶
public static Result<T> Success(T value)
Parameters:
- value
: The value to hold in the result.
Returns:
A new successful Result<T>
instance.
Example:
var success = Result<int>.Success(42);
Failure¶
public static Result<T> Failure(Error error)
public static Result<T> Failure(ErrorCode code, string message)
Parameters:
- error
: The error to hold in the result.
- code
: The error code.
- message
: The error message.
Returns:
A new failed Result<T>
instance.
Example:
var failure = Result<int>.Failure(ErrorCode.NotFound, "Value not found");
FromTry¶
public static Result<T> FromTry(Func<T> func, Func<Exception, Error> errorFactory)
Parameters:
- func
: The function to execute.
- errorFactory
: The function to create an error from an exception.
Returns:
A new Result<T>
instance.
Example:
var result = Result<int>.FromTry(
() => int.Parse("42"),
ex => Error.Create(ErrorCode.InvalidOperation, ex.Message)
);
Instance Methods¶
Map¶
public Result<TResult> Map<TResult>(Func<T, TResult> mapper)
Parameters:
- mapper
: The function to transform the value.
Returns: A new result with the transformed value or the original error.
Example:
var result = Result<int>.Success(42)
.Map(x => x * 2);
Bind¶
public Result<TResult> Bind<TResult>(Func<T, Result<TResult>> binder)
Parameters:
- binder
: The function to bind the result.
Returns: A new result with the bound value or the original error.
Example:
var result = Result<int>.Success(42)
.Bind(x => Result<string>.Success(x.ToString()));
Match¶
public TResult Match<TResult>(Func<T, TResult> onSuccess, Func<Error, TResult> onFailure)
Parameters:
- onSuccess
: The function to handle success.
- onFailure
: The function to handle failure.
Returns: The result of the appropriate handler function.
Example:
var message = result.Match(
value => $"Success: {value}",
error => $"Error: {error.Message}"
);
OnSuccess¶
public Result<T> OnSuccess(Action<T> action)
Parameters:
- action
: The action to execute.
Returns: The original result.
Example:
result.OnSuccess(value => Console.WriteLine($"Value: {value}"));
OnFailure¶
public Result<T> OnFailure(Action<Error> action)
Parameters:
- action
: The action to execute.
Returns: The original result.
Example:
result.OnFailure(error => Console.WriteLine($"Error: {error.Message}"));
Validate¶
public Result<T> Validate(Func<T, bool> predicate, ErrorCode errorCode, string errorMessage)
Parameters:
- predicate
: The predicate to validate the value.
- errorCode
: The error code to use if validation fails.
- errorMessage
: The error message to use if validation fails.
Returns: A new result with the validated value or a failed result if validation fails.
Example:
var result = Result<int>.Success(42)
.Validate(x => x > 0, ErrorCode.ValidationError, "Value must be positive");
Catch¶
public Result<T> Catch(Func<Error, Result<T>> handler)
Parameters:
- handler
: The function to handle the error.
Returns: A new result with the handled value or the original error.
Example:
var result = Result<int>.Failure(ErrorCode.NotFound, "Not found")
.Catch(error => Result<int>.Success(0));
GetValueOr¶
public T GetValueOr(T defaultValue)
public T GetValueOr(Func<T> func)
Parameters:
- defaultValue
: The default value to return if the result has failed.
- func
: The function to get the default value if the result has failed.
Returns: The value of the result if it is successful, or the default value if it has failed.
Example:
var value = result.GetValueOr(0);
var value = result.GetValueOr(() => GetDefaultValue());
GetValueOrThrow¶
public T GetValueOrThrow()
public T GetValueOrThrow(Func<Error, Exception> exceptionFactory)
Parameters:
- exceptionFactory
: The function to create an exception from an error.
Returns: The value of the result if it is successful.
Throws: An exception if the result has failed.
Example:
try
{
var value = result.GetValueOrThrow();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
Static Utility Methods¶
Combine¶
public static Result<(T, T2)> Combine<T2>(Result<T> result1, Result<T2> result2)
public static Result<IEnumerable<T>> Combine(IEnumerable<Result<T>> results)
Parameters:
- result1
: The first result.
- result2
: The second result.
- results
: The results to combine.
Returns: A new result with all values or the first error encountered.
Example:
var result1 = Result<int>.Success(42);
var result2 = Result<string>.Success("Hello");
var combined = Result.Combine(result1, result2);
var results = new[] { result1, result2 };
var allResults = Result.Combine(results);