Kekka 0.0.3
dotnet add package Kekka --version 0.0.3
NuGet\Install-Package Kekka -Version 0.0.3
<PackageReference Include="Kekka" Version="0.0.3" />
paket add Kekka --version 0.0.3
#r "nuget: Kekka, 0.0.3"
// Install Kekka as a Cake Addin #addin nuget:?package=Kekka&version=0.0.3 // Install Kekka as a Cake Tool #tool nuget:?package=Kekka&version=0.0.3
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 - 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.TryGetValue(out var value))
{
Console.WriteLine($"Success: {value}");
}
if (result2.TryGetError(out var error))
{
Console.WriteLine($"Failure: {error}");
}
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.TryGetValue(out var value))
{
Console.WriteLine($"result1: {value}"); // Output: result1: 6
}
Example 2: Handling failure
In the following example, one of the operations fails (Result.Error<T, TError>
), 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.TryGetError(out var error))
{
Console.WriteLine($"result2: {error.Message}"); // Output: result2: Error!!
}
Asynchronous Support
You can also use asynchronous results with Task<Result<T, TError>>
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.TryGetValue(out var value))
{
Console.WriteLine($"Async result: {value}"); // Output: Async result: 25
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions 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. 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. |
.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. |
-
.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.