Kekka 0.0.2

dotnet add package Kekka --version 0.0.2                
NuGet\Install-Package Kekka -Version 0.0.2                
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="Kekka" Version="0.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Kekka --version 0.0.2                
#r "nuget: Kekka, 0.0.2"                
#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 Kekka as a Cake Addin
#addin nuget:?package=Kekka&version=0.0.2

// Install Kekka as a Cake Tool
#tool nuget:?package=Kekka&version=0.0.2                

Kekka - Result Type for C#

Kekka - means result in Japanese - is a C# library that provides a functional-style Result type, enabling you to handle success and failure cases more elegantly in your applications. Inspired by the Railway Oriented Programming (ROP) concept, Kekka helps you structure your code in a way that keeps it clean and handles errors gracefully.

Features

  • Functional-style Result type (Ok, Error)
  • Extension methods for LINQ-style composition
  • Support for asynchronous operations
  • Tools for Railway Oriented Programming (ROP)

Installation

You can install Kekka from NuGet:

dotnet add package Kekka

Usage

Basic Result Type Usage

using Kekka;

var result1 = Result.Ok<int, string>(10);  // Success case
var result2 = Result.Error<int, string>("Something went wrong");  // Error case

if (result1 is Ok<int, string> ok)
{
    Console.WriteLine($"Success: {ok.Value}");
}
else if (result2 is Error<int, string> error)
{
    Console.WriteLine($"Failure: {error.Value}");
}

Railway Oriented Programming (ROP) with LINQ

Railway Oriented Programming is a concept introduced by Scott Wlaschin, designed to handle success and failure cases in a clear, chainable manner. In Kekka, you can use LINQ and extension methods like SelectMany to compose multiple operations that can succeed or fail.

Example 1: All operations succeed
using Kekka;

var result1 = from x in Result.Ok<decimal, Exception>(2)
              from y in Result.Ok<decimal, Exception>(x)
              from z in Result.Ok<decimal, Exception>(y)
              select x + y + z;

if (result1 is Ok<decimal, Exception> ok)
{
    Console.WriteLine($"result1: {ok.Value}");  // Output: result1: 6
}
Example 2: Handling failure

In the following example, one of the operations fails (Result.Error<TSuccess, TFaulure>), and the chain stops immediately, returning the error.

using Kekka;

var result2 = from x in Result.Ok<decimal, Exception>(2)
              from y in Result.Error<decimal, Exception>(new Exception("Error!!"))
              from z in Result.Ok<decimal, Exception>(3)
              select x + y + z;

if (result2 is Error<decimal, Exception> error)
{
    Console.WriteLine($"result2: {error.Value.Message}");  // Output: result2: Error!!
}
Asynchronous Support

You can also use asynchronous results with Task<Result<TSuccess, TFailure>> and chain them using the provided extension methods.

using Kekka;
using System.Threading.Tasks;

var result = await from x in Task.FromResult(Result.Ok<int, string>(10))
                   from y in Task.FromResult(Result.Ok<int, string>(x + 5))
                   select x + y;

if (result is Ok<int, string> ok)
{
    Console.WriteLine($"Async result: {ok.Value}");  // Output: Async result: 25
}

API Reference

Result<TSuccess, TFailure>

  • The base abstract class representing a result that can either be a success or a failure.

Ok<TSuccess, TFailure>

  • A sealed class representing a successful result.
  • Property:
    • TSuccess Value: The success value.

Error<TSuccess, TFailure>

  • A sealed class representing a failure result.
  • Property:
    • TFailure Value: The failure value.

Result.Ok<TSuccess, TFailure>(TSuccess value)

  • A static method to create a successful result with the provided success value.

Result.Error<TSuccess, TFailure>(TFailure error)

  • A static method to create a failure result with the provided error value.

Extension Methods

The library provides several useful extension methods to work with Result<TSuccess, TFailure> types in a more functional way.

  • Select - Map over the success value.
  • SelectMany - Chain multiple Result instances.
  • MapError - Map over the failure value.
  • ToAsyncResult - Convert a Result to an asynchronous Task<Result<TSuccess, TFaulure>>.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.1

    • 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.0.2 33 11/28/2024
0.0.1 109 10/3/2024