Ametrin.Optional 0.1.1

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

// Install Ametrin.Optional as a Cake Tool
#tool nuget:?package=Ametrin.Optional&version=0.1.1                

Ametrin.Optional

A simple C#/.NET library containing various allocation free option types written by Barion. This is a rewrite of the Optional namespace in Ametrin.Utils.<br> Warning: This is my 3rd take on options and I assume it will not be my last (Especially once .NET gets type unions). I try to avoid breaking changes. If I have to break something I'll create a legacy branch.

dotnet add package Ametrin.Optional

Types

Option<T>

T or Error

Option<T> a = someT; // equivalent to Option.Of(someT) - will produce Error if someT is null
Option<T> b = default; // equivalent to Option.Error<T>() 
//careful that default is actually referencing Option<T> and not T. Especially in conditional assignments.
Option<T> a = Option.Success(someT); //requires a nonnull value

Option

Success or Error

Option success = true; // equivalent to Option.Success()
Option error = false; //equivalent to Option.Error()

Result<T, E>

T or E

Result<T, E> success = someT; // equivalent to Result.Success<T, E>(someT)  
Result<T, E> error = someE; // equivalent to Option.Error<T, E>(someE) 

Result<T>

T or Exception

ErrorState<T>

Success or T

ErrorState

Success or Exception

General API

All option types in this library have a monadic, linq-like api to interact with them.

Examples

option.Select(value => value.ToString()).WhereNot(string.IsNullOrWhiteSpace).Or("John Doe");
option.Where(a => a > 5).Select(a => a * 5).Consume(a => process(a), () => reportFailure());
(optionA, optionB).Select((a, b) => a * b);
(optionA, optionB).Consume((a, b) => ...);

There is an alternative most Try... methods that return an option. If not, I'm happy to accept pull requests.<br> If you are missing something feel free to create an issue or PR (talk to me before so i agree on the design)

Edge Cases

If you run into edge cases that are not covered by the API let me know so we can find a solution<br> In the mean time you can use the OptionsMarshall to get direct access to the underlying data. This can also be used in high performance scenarious where the delegate cannot be static

if(OptionsMarshall.IsSuccess(result))
{
    ...
}

if(OptionsMarshall.TryGetError(result, out var error))
{
    ...
}

if(OptionsMarshall.TryGetValue(result, out var value))
{
    ...
}

Unit tests

If you need to unit test an option you can use Ametrin.Optional.Testing.TUnit to to simplify the testing experience with TUnit. Feel free to contribute similar extensions for your testing framework

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Ametrin.Optional:

Package Downloads
Ametrin.Optional.Testing.TUnit

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.1 68 12/21/2024
0.1.0 117 11/27/2024