Struct Result<T>
- Namespace
- Whim
- Assembly
- Whim.dll
Result
is similar to Rust's Result<T, E>
type, and DotNext's Result<T, Exception>
.
It represents the outcome of an operation that can either succeed with a value of type T or fail with an error.
This type is used to encapsulate the result of operations in a way that allows for error handling without exceptions.
It provides methods to check success, retrieve values, and handle errors.public readonly struct Result<T> : IEquatable<Result<T>>
Type Parameters
T
- The type of the value contained in the result.
- Implements
-
IEquatable<Result<T>>
- Inherited Members
Constructors
Result(WhimError)
Creates a failed result with the specified error.
public Result(WhimError error)
Parameters
error
WhimError- The error to wrap in the result.
Result(T)
Creates a successful result with the specified value.
public Result(T value)
Parameters
value
T- The value to wrap in the result.
Properties
Error
Gets the error if the operation failed, otherwise returns null.
public WhimError? Error { get; }
Property Value
IsSuccessful
Gets a value indicating whether the result represents a successful operation.
public bool IsSuccessful { get; }
Property Value
Value
Gets the value if the operation was successful, otherwise throws an exception.
public T? Value { get; }
Property Value
- T
Exceptions
- InvalidOperationException
- Thrown when the result represents a failure.
ValueOrDefault
Gets the value if the operation was successful, otherwise returns the default value for T.
public T? ValueOrDefault { get; }
Property Value
- T
Methods
Equals(object?)
Determines whether the specified object is equal to the current result.
public override bool Equals(object? obj)
Parameters
obj
object- The object to compare with the current result.
Returns
- bool
- true if the specified object is equal to the current result; otherwise, false.
Equals(Result<T>)
Determines whether the current result is equal to another result of the same type.
public bool Equals(Result<T> other)
Parameters
other
Result<T>- The result to compare with the current result.
Returns
GetHashCode()
Returns the hash code for this result.
public override int GetHashCode()
Returns
- int
- A hash code for the current result.
OrInvoke(Func<T>)
If the result is successful, returns the value; otherwise, invokes the specified function.
public T OrInvoke(Func<T> func)
Parameters
func
Func<T>- The function to invoke if the result is not successful.
Returns
- T
- The value if the result is successful; otherwise, the result of invoking the specified function.
TryGet(out T)
Attempts to get the value from the result.
public bool TryGet(out T value)
Parameters
value
T- When this method returns, contains the value if the operation was successful; otherwise, the default value for T.
Returns
- bool
- true if the operation was successful; otherwise, false.
Operators
operator ==(Result<T>, Result<T>)
Determines whether two specified results are equal.
public static bool operator ==(Result<T> left, Result<T> right)
Parameters
Returns
- bool
- true if the results are equal; otherwise, false.
implicit operator Result<T>(WhimError)
Implicitly converts an error to a failed result.
public static implicit operator Result<T>(WhimError error)
Parameters
error
WhimError- The error to convert.
Returns
- Result<T>
implicit operator Result<T>(T)
Implicitly converts a value to a successful result.
public static implicit operator Result<T>(T value)
Parameters
value
T- The value to convert.
Returns
- Result<T>
operator !=(Result<T>, Result<T>)
Determines whether two specified results are not equal.
public static bool operator !=(Result<T> left, Result<T> right)
Parameters
Returns
- bool
- true if the results are not equal; otherwise, false.