SharpResult 1.0.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package SharpResult --version 1.0.9                
NuGet\Install-Package SharpResult -Version 1.0.9                
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="SharpResult" Version="1.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpResult --version 1.0.9                
#r "nuget: SharpResult, 1.0.9"                
#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.
// Install SharpResult as a Cake Addin
#addin nuget:?package=SharpResult&version=1.0.9

// Install SharpResult as a Cake Tool
#tool nuget:?package=SharpResult&version=1.0.9                

Sharp Result

Inspired by:

Features:

  • Easily box and unbox error values.
    • Success results are implicitly converted to success results
    • Error types can be created without specifying generics.
    • Match can be used to convert both success and errors to the same unwrapped type
    • Unwrap can be used to unsafely get the successful values
  • Easily chain together results (Railway oriented programming with Bind)

Examples

Example unboxing results

using static SharpResult.Result;

public class ResultTests
{
    [Fact]
    public void BasicErrorConstruction()
    {
        Result<int, string> result = Error("error");
        Assert.True(result.IsError);
        Assert.Equal("error", result.Error);
        Assert.Equal("error", result.Match(ok => ok.ToString(), err => err));
    }

    [Fact]
    public void BasicOkConstruction()
    {
        Result<int, string> result = 100;
        Assert.False(result.IsError);
        Assert.True(result.IsOk);
        Assert.Equal(100, result.Ok);
        Assert.Equal(100, result.Match(ok => ok, err => throw new System.Exception("unreachable")));
    }
}

Example bind usage

public string HandleRequest(string request) =>
    validate(request)
        .Bind(canonicalizeEmail)
        .Bind(trySaveRequest)
        .Bind(_ => "ok")
        .Match(ok => ok, err => err);

private static Result<string, string> validate(string request) { }
private static Result<string, string> trySaveRequest(string arg) { }
// bind can take either functions taking / returning results, OR functions taking the success / error values
private static string canonicalizeEmail(string request) { } 

Without bind

// without bind
public string HandleRequestWithoutBind(string request)
{
    var isValid = validateRequest(request);
    if (!isValid)
    {
        return "Error: validation";
    }

    canonicalizeEmail(request);

    try 
    {
        saveRequest(request);
    }
    catch
    {
        return "Error: save";
    }

    return "ok";
}

Create result types from functions throwing exceptions

[Fact]
public void Try_FunctionsReturningValuesAreConvertedToOk()
{
    string okFunc() => "ok";

    var tryOk = Result.Try(okFunc);
    Assert.True(tryOk.IsOk);
    Assert.Equal(okFunc(), tryOk.Unwrap());
}

[Fact]
public void Try_FunctionsThrowingExceptionsAreConvertedToErrors()
{
    string errorFunc() => throw new System.Exception("error");

    var tryError = Result.Try(errorFunc);
    Assert.False(tryError.IsOk);
    Assert.Equal("error", tryError.Error.Message);
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.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
1.0.12 486 1/18/2022
1.0.11 419 1/18/2022
1.0.10 318 11/11/2021
1.0.9 332 11/4/2021
1.0.8 333 10/26/2021
1.0.7 301 10/25/2021
1.0.6 341 10/25/2021
1.0.5 344 10/21/2021
1.0.4 298 10/21/2021
1.0.3 338 10/20/2021
1.0.2 333 10/20/2021
1.0.1 352 10/20/2021
1.0.0 391 10/20/2021