RResult 0.2.0

dotnet add package RResult --version 0.2.0
                    
NuGet\Install-Package RResult -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RResult" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RResult" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="RResult" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RResult --version 0.2.0
                    
#r "nuget: RResult, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RResult@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RResult&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=RResult&version=0.2.0
                    
Install as a Cake Tool

RResult

ci

A Result type for modelling 2way pipeline for C Sharp.

Introduction

The Result type is a monad type that holds return values and errors. By holding the state after an operation, such as a function, in the Result type, the result of the execution can be clearly defined. If an element T is found, the successful value can be retrieved from Ok<T>, and if an error is found, the failed result can be retrieved from Err<E>. This avoids ambiguity in the handling of possible value absences such as null, or complications in the processing path due to exception throws.

The Result type of this project is represented as type RResult. The initials R are derived from Rust and Railway Oriented Programming.

Gettings Started

dotnet add package RResult

This is a FizzBuzz with Result type. It can be described in a declarative, flow-controlled, natural language alike, showing that it can be coded simply and robustly. Full sample is ./example/RResult.FizzBuzz

    static void Main(string[] args) =>
        Console.WriteLine(
            string.Join(",", Enumerable.Range(1, 30).Select(Pipeline))
        );

    static string Pipeline(int i) =>
        DoDivide(i, 15, "FizzBuzz")
            .OrElse(it => DoDivide(it, 3, "Fizz"))
            .OrElse(it => DoDivide(it, 5, "Buzz"))
            .MapBoth(
                Ok => $"{Ok}",
                Err => $"{Err}"
            );

    static RResult<string, int> DoDivide(int dividend, int divisor, string sig) =>
        (dividend % divisor) switch
        {
            0 => sig,
            _ => dividend,
        };

Transforming Results

Both success/error values can be converted through Map / MapErr.

RResult<int, SomeErr>.Ok(3) // Ok(3)
    .Map(a => a * a) // Ok(9))
    // Also you can convert type int to string
    .Map(n => $"{n}") // OK("9")

Mehod Chain

Results can be chain.

image-of-rresult-concepts-and-codes

Async Task Function

Also some functions are support AsyncFunction. Will be fully compliant in the future.

  • MapAsync / MapErrAsync
  • InspectAsync / InspectErrAsync
  • AndThenAsync / AndThenErrAsync

Combine Result

Not currently supported, but will be in the future

Inspiration

Inspiration for this library has been drawn:

  • Rust
  • kotlin-result

Improvements on existing solutions

  • Feature parity with Result types from other languages including Rust
  • Lax constraints on success value nullability
  • Consistent naming with existing Result libraries from other languages (e.g. map, mapError, mapBoth, and, andThen, or, orElse, unwrap)
  • Unit tests covering every library method

Comparison table with other projects

Function description RResult Rust kotlin-result
Ok Make Ok Result instance
Err Make Error Result instance
isErr Returns true if the result is Err.
isOk Returns true if the result is Ok.
And Returns res if the result is Ok, otherwise returns the Err value of self
Or Returns res if the result is Err, otherwise returns the Ok value of self.
andThen Calls op if the result is Ok, otherwise returns the Err value of self.<br><br>This function can be used for control flow based on Result values.
orElse Calls op if the result is Err, otherwise returns the Ok value of self.
unwrap Returns the contained Ok value, consuming the self value.
unwrapErr Returns the contained Err value, consuming the self value.
Map Maps a Result<T, E> to Result<U, E> by applying a function to a contained Ok value, leaving an Err value untouched.
Inspect Calls a function with a reference to the contained value if Ok
InspectErr Calls a function with a reference to the contained value if Erro
ToResultOr Covert Result Or TBD
RunCatching, mapError Catch exception and return e.message TBD

Contributing

Bug reports and pull requests are welcome on [GitHub][github].

License

This project is available under the terms of the Apache ver 2.0 license. See the LICENSE file for the copyright information and licensing terms.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 165 7/10/2024
0.1.6 163 6/15/2024
0.1.5 159 6/9/2024
0.1.0 152 6/9/2024