Struct Result<TValue>
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Represents either a successful computation (with a value) or a failure (with an Error).
public readonly struct Result<TValue> : IResult<TValue>, IResult, IEquatable<Result<TValue>>
Type Parameters
TValueSuccess value type.
- Implements
-
IResult<TValue>IEquatable<Result<TValue>>
- Inherited Members
- Extension Methods
Examples
// Creating results
Result<User> success = Result.Success(user);
Result<User> failure = Error.NotFound("User not found");
// Pattern matching
var message = result switch
{
{ IsSuccess: true } => $"Found user: {result.Value.Name}",
{ IsFailure: true } => $"Error: {result.Error.Detail}"
};
// Chaining operations
var finalResult = GetUser(id)
.Bind(user => ValidateUser(user))
.Map(user => user.Name);
Remarks
Result is the core type for Railway Oriented Programming. It forces explicit handling of both success and failure cases, making error handling visible in the type system. Use Result when an operation can fail in a predictable way that should be handled by the caller.
Properties
Error
Gets the error if the result is a failure; otherwise throws.
public Error Error { get; }
Property Value
- Error
The error describing why the result failed.
Remarks
Always check IsFailure before accessing this property, or use TryGetError(out Error) instead.
Exceptions
- InvalidOperationException
Thrown when accessing Error on a successful result.
IsFailure
True when the result represents failure.
public bool IsFailure { get; }
Property Value
- bool
True if failed; otherwise false.
IsSuccess
True when the result represents success.
public bool IsSuccess { get; }
Property Value
- bool
True if successful; otherwise false.
Value
Gets the underlying value if the result is successful; otherwise throws.
public TValue Value { get; }
Property Value
- TValue
The success value.
Remarks
Always check IsSuccess before accessing this property, or use TryGetValue(out TValue) instead.
Exceptions
- InvalidOperationException
Thrown when accessing Value on a failed result.
Methods
Deconstruct(out bool, out TValue?, out Error?)
Deconstructs the result into its components for pattern matching.
public void Deconstruct(out bool isSuccess, out TValue? value, out Error? error)
Parameters
isSuccessboolTrue if the result is successful; otherwise false.
valueTValueThe success value if successful; otherwise default.
errorErrorThe error if failed; otherwise null.
Examples
var (success, value, error) = GetUser(id);
if (success)
Console.WriteLine($"User: {value.Name}");
else
Console.WriteLine($"Error: {error.Detail}");
Equals(Result<TValue>)
Determines whether the specified result is equal to the current result.
public bool Equals(Result<TValue> other)
Parameters
otherResult<TValue>The result to compare with the current result.
Returns
- bool
True if the specified result is equal to the current result; otherwise false.
Remarks
Two results are equal if they have the same success/failure state and equal values/errors.
Equals(object?)
Determines whether the specified object is equal to the current result.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current result.
Returns
- bool
True if the specified object is a Result and is equal to the current result; otherwise false.
GetHashCode()
Returns a hash code for the current result.
public override int GetHashCode()
Returns
- int
A hash code for the current result.
ToString()
Returns a string representation of the result.
public override string ToString()
Returns
- string
A string in the format "Success(value)" or "Failure(ErrorCode: detail)".
TryGetError(out Error)
Attempts to get the error without throwing.
public bool TryGetError(out Error error)
Parameters
errorErrorWhen this method returns true, contains the error; otherwise, null.
Returns
- bool
True if the result is a failure; otherwise false.
Remarks
This is the recommended safe way to access the error without exception handling.
TryGetValue(out TValue)
Attempts to get the success value without throwing.
public bool TryGetValue(out TValue value)
Parameters
valueTValueWhen this method returns true, contains the success value; otherwise, the default value.
Returns
- bool
True if the result is successful; otherwise false.
Remarks
This is the recommended safe way to access the value without exception handling. Similar to the TryParse pattern in .NET.
Operators
operator ==(Result<TValue>, Result<TValue>)
Determines whether two results are equal.
public static bool operator ==(Result<TValue> left, Result<TValue> right)
Parameters
Returns
- bool
True if the results are equal; otherwise false.
implicit operator Result<TValue>(Error)
Implicitly converts an error to a failed result.
public static implicit operator Result<TValue>(Error error)
Parameters
errorErrorThe error to wrap in a failed result.
Returns
- Result<TValue>
A failed result containing the error.
implicit operator Result<TValue>(TValue)
Implicitly converts a value to a successful result.
public static implicit operator Result<TValue>(TValue value)
Parameters
valueTValueThe value to wrap in a success result.
Returns
- Result<TValue>
A successful result containing the value.
operator !=(Result<TValue>, Result<TValue>)
Determines whether two results are not equal.
public static bool operator !=(Result<TValue> left, Result<TValue> right)
Parameters
Returns
- bool
True if the results are not equal; otherwise false.