OptionSharp 0.3.1
dotnet add package OptionSharp --version 0.3.1
NuGet\Install-Package OptionSharp -Version 0.3.1
<PackageReference Include="OptionSharp" Version="0.3.1" />
<PackageVersion Include="OptionSharp" Version="0.3.1" />
<PackageReference Include="OptionSharp" />
paket add OptionSharp --version 0.3.1
#r "nuget: OptionSharp, 0.3.1"
#:package OptionSharp@0.3.1
#addin nuget:?package=OptionSharp&version=0.3.1
#tool nuget:?package=OptionSharp&version=0.3.1
OptionSharp
Another option for options in C#
Disclaimer: If you are looking for a stable, production ready lib you are better off using something like LanguageExt.Core.
Motivation
This library is primarily for myself to learn about functional programming but also takes a different opinion/approach compared to other more well established libs.
- Focuses only on Option and Result types
- Does not allow for null
- Uses records as discriminated unions which is as close as you can get in c# whilst still being idiomatic (AFAIK)
- Rust-like (ish) feel, promotes the use of strongly typed errors
- Source generated code to keep it readable
Usage
Define your error types as below which will generate some constructor methods for convenience
[GenerateResult("Handle")]
public abstract record HandleError;
public sealed record FailedToDeserialize(string Message) : HandleError;
public sealed record InvalidId(int Id) : HandleError;
Return options and results in your domain
public Result<int, HandleError> TryGetIdOnlyIfSeven(int id) =>
id == 7
? Handle.Ok(id)
: Handle.Err<int>(new InvalidId(id));
Use extension methods to operate on values in a functional way
var eight = TryGetIdOnlyIfSeven(7)
.Inspect(seven => Console.WriteLine($"seven is {seven}"))
.Map(seven => seven + 1)
.UnwrapOrDefault(0);
| Product | Versions 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. |
-
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.3.1 | 224 | 4/30/2025 |
| 0.2.5 | 210 | 8/24/2024 |
| 0.2.4 | 178 | 6/17/2024 |
| 0.2.1 | 169 | 5/10/2024 |
| 0.1.3-alpha | 149 | 2/25/2024 |
| 0.1.0-alpha | 145 | 2/11/2024 |