Rop.Results9 is a result/error discriminated union for .NET9

Ramon Ordiales Plaza - Oct 7 - - Dev Community

Rop.Results9 is a library designed to return either a specific result or an error from an operation avoiding the need to use exceptions.

It is available as a Nuget package as Rop.Results9 and the source code is available on github at Rop.Results9

Results can be of 3 types:

  • VoidResult: Where only success or error in the operation is returned

  • Result: Where error or a specific result of type T is returned

  • EnumerableResult<T>: Where error or an enumerable result of type T is returned

This library use implicit operators, it is not necessary to cast in the return, the result is simply returned normally, or the corresponding error is sent

Example:

public Result<int> Divide1(int dividend, int divisor)
{
  if (divisor == 0)
  {
    return Error.Fail("Cannot divide by zero.");
  }
  else
  {
    return dividend / divisor;
  }
}
Enter fullscreen mode Exit fullscreen mode

To process this result, there are several ways to do it. The most direct way is to check if the result has failed and send the error upwards, otherwise use the returned value:

public VoidResult ShowDivide2(int divident,int divisor)
{
  var result = Divide1(divident, divisor);
  if (result.IsFailed)
    return result;
  Console.WriteLine($"The result is {result.Value!}");
  return VoidResult.Ok;
}
Enter fullscreen mode Exit fullscreen mode

You can also map the result between a correct value and an error.

public VoidResult ShowDivide(int divident,int divisor)
{
  var result = Divide1(divident, divisor);
  return result.Map(
            v => Console.WriteLine($"The result is {v}"),
            e => Console.WriteLine($"Error: {e.Value}")
  );
}
Enter fullscreen mode Exit fullscreen mode

The library is very complete and includes several possible scenarios. The documented code is on GitHub, but here is a brief summary:

Classes:

  • CancelError: Represents an error that occurs when an operation is cancelled.
  • CastError: Represents an error that occurs when an operation is not castable.
  • EmptyError: Represents an error that occurs when the result value is empty.
  • Error: Represents an error in the result object.
  • Error: Represents an error object with a data associated.
  • ExceptionError: Represents an error that occurred due to an exception.
  • FailError: Represents an error that occurs when an operation fails.
  • MultiError: Represents an error that contains multiple errors.
  • NotEnumerableError: Represents an error that occurs when an operation is not enumerable.
  • NullError: Represents an error that occurs when the result value is null.
  • TimeOutError: Represents an error due to a timeout.
  • UnknownError: Represents an unknown error.

  • Result: Static class containing various methods for working with Result<T> objects.

  • Result<T>: Represents a result with a value that can either be successful or contain an error.

  • EnumerableResult<A>: Represents a result of an operation that returns an enumerable value.

  • ResultHelper: A static class containing extension methods for the Result<T> class.

  • VoidResult: Represents a result that does not contain a value. But can be successful or failed.

IResult Interface

Represents the result of an operation that can either succeed or fail.

Properties:

  • Error: Gets the error associated with this instance, if any.
  • IsFailed: Gets a value indicating whether this instance is fail.
  • IsFailedAndNotNull: Gets a value indicating whether this instance is failed and a null value is not the cause.
  • IsNull: Gets a value indicating whether this instance is null.
  • IsOk: It is an alias for IsSuccessful
  • IsOkOrNull: Returns true if the result is successful or null.
  • IsSuccessful: Gets a value indicating whether this instance is successful.

Methods:

  • GetObjectValueOrDefault(): Directly returns the value of the result as an object.
  • ThrowIfFailed(): Throws an exception if this instance is fail.

Result<T> Class

Represents a result with a value that can either be successful or contain an error.

The Result<T> type exposes the following members.

Constructors:

  • Result(T): Initializes a new instance of the Result class with a value.
  • Result(Error): Initializes a new instance of the Result<T> class with an error.

Properties:

  • Error: Gets the error value.
  • Value: Gets the result value.

Methods:

  • AsEnumerable<A>(): Converts the result to an enumerable result.
  • Deconstruct(ref T, ref Error): Deconstructs the result into its value and error.
  • Equals(Object): Determines whether this result is equal to an object.
  • Equals(Result<T>): Determines whether this result is equal to another result.
  • Equals(T): Determines whether this result value is equal to another value.
  • Execute(Action<T>): Executes an action ;
  • Execute(Func<T,Boolean>): Executes an action if the result is successful. If the action throws an exception or returns false, the result is an error.
  • Execute(Func<T,VoidResult>): Executes an action if the result is successful. If the action throws an exception the result is an error. Otherwise, the result is the result of the action.
  • FromError(Error): Creates a new instance of the Result<T> class with the specified error.
  • FromValue(T): Creates a new instance of the Result<T> class with the specified value.
  • GetObjectValueOrDefault(): Protected method to get the object value or null.
  • IfFailed(T): Alias for ValueOrDefault
  • IfFailed(Func<Error,T>): Returns a successful result with the specified function in case of isfailed.
  • Map<B>(Func<T,B>): Executes an scalar function if the result is successful.
  • Map<B>(B): Maps the result value to a new result value.
  • Map(Action<T>, Func<Error,Error>): Maps the result value to a new voidresult value.
  • Map(Action<T>, Action<Error>): Maps the result value to a new voidresult value.
  • MapEnumerable<A>(Func<T,IEnumerable<A>>): Executes an scalar function that returns an ienumerable if the result is successful.
  • MapFailed(T): Returns a successful result with the specified value in case of isfailed.
  • Match<B>(Func<T,B>, Func<Error,B>): Matches the result value to a new value or error.
  • Match<B>(Func<T,B>, B): Matches the result value to a new value.
  • Match<B>(B, B): Matches the result value to a new value.
  • TryGet(ref T): Try to get the result value.
  • ValueOrDefault(T): Returns the result value or defaultvalue if the result is a failure.
  • ValueOrThrow(): Gets the result value or throws an exception if the result is a failure or null.
  • WithError(Error): Instantiates a new result with the specified error.
  • WithValue(T): Instantiates a new result with the specified value.

EnumerableResult Class

Represents a result of an operation that returns an enumerable value.

Constructors:

  • EnumerableResult(IEnumerable<A>): Creates a new instance of the EnumerableResult<A> class with the specified ienumerable value.
  • EnumerableResult(A, params A[]): Creates a new instance of the EnumerableResult<A> class with params items
  • EnumerableResult(Error): Creates a new instance of the EnumerableResult<A> class with the specified error.

Properties:

  • IsEmpty: EnumerableResult is empty.
  • IsFailedOrEmpty: EmumerableResult is failed or empty.
  • IsNullOrEmpty: Alias for IsFailedOrEmpty
  • Value: Gets the result value as a read-only list.
  • Error: Gets the error value.

Methods:

  • AsEnumerable(): Converts the result to an enumerable.
  • AsReadOnlySpan: Converts the enumerable result to a read-only span.
  • Deconstruct(ref IReadOnlyList<A>, ref Error): Decomposes the result into a value and an error.
  • Equals(Object): Determines whether the specified object is equal to the current object.
  • Equals(EnumerableResult<A>): Determines whether two EnumerableResults are equal.
  • Equals(IEnumerable<A>): Determines whether this EnumerableResult is equal to an IEnumerable.
  • Execute(Action<IReadOnlyList<A>>): Executes the specified action if the result is successful.
  • Execute(Func<IReadOnlyList<A>,VoidResult>): Executes the specified function if the result is successful.
  • Execute(Func<IReadOnlyList<A>,Boolean>): Executes the specified function if the result is successful.
  • ExecuteScalar<T>(Func<IReadOnlyList<A>,T>): Executes the specified scalar function if the result is successful.
  • First(Func<A,Boolean>): Returns the first item that matches the specified function if the result is successful.
  • First(): Returns the first item if the result is successful.
  • FirstOrDefault(Func<A,Boolean>): Returns the first item that matches the specified function if the result is successful otherwise returns default value.
  • FirstOrDefault(): Returns the first item if the result is successful otherwise returns default value.
  • ForEach(Action<A>): Executes the specified action for each item in the result value if the result is successful.
  • FromError(Error): Creates a new instance of the EnumerableResult<A> class with the specified error.
  • FromResult(Result<IEnumerable<A>>): Converts a Result of an IEnumerable to a EnumerableResult<A>.
  • FromValue(IEnumerable<A>): Creates a new instance of the EnumerableResult<A> class with the specified ienumerable value.
  • FromValue(A): Creates a new instance of the EnumerableResult<A> class with the specified single value.
  • GetObjectValueOrDefault(): protected override object GetObjectValueOrDefault()
  • IfFailed(Func<Error,IReadOnlyList<A>>): Converts the result to a list, or given a function to convert the error.
  • Map<T>(Func<IReadOnlyList<A>,IEnumerable<T>>): Maps the result value to a new result value using a function.
  • Map<T>(Func<A,T>): Maps each item using the specified function foreach item if the result is successful.
  • Map(Action<IReadOnlyList<A>>, Func<Error,Error>): Maps the result value to a new voidresult value.
  • Map(Action<IReadOnlyList<A>>, Action<Error>): Maps the result value to a new voidresult value.
  • Map<B>(B): Maps the successful result to a new result using the specified value;
  • Match<B>(Func<IReadOnlyList<A>,B>, Func<Error,B>): Matches the result value to a new value or error using two functions.
  • Match<B>(Func<A,B>, Func>Error,IReadOnlyList<B>>): Matches the result value to a new list value using a foreach function or error using a list function.
  • MatchOrNull: Returns the result value or null if the result is a failure.
  • Select<B>(Func<A,B>): Alias for Map. Selects each item using the specified function if the result is successful.
  • ToArray(): Converts the result to an array.
  • ToList(): Converts the result to a list.
  • ValueOrThrow(): Gets the result value or throws an exception if the result is a failure.
  • Where(Func<A,Boolean>): Filters the items using the specified function if the result is successful.
  • WithError(Error): Initializes a new instance of the EnumerableResult<A> class with the specified error.
  • WithValue(IEnumerable<A>): Initializes a new instance of the EnumerableResult<A> class with the specified value.

This post was also published on Medium.

. .
Terabox Video Player