Ametrin.Optional
0.1.0
See the version list below for details.
dotnet add package Ametrin.Optional --version 0.1.0
NuGet\Install-Package Ametrin.Optional -Version 0.1.0
<PackageReference Include="Ametrin.Optional" Version="0.1.0" />
paket add Ametrin.Optional --version 0.1.0
#r "nuget: Ametrin.Optional, 0.1.0"
// Install Ametrin.Optional as a Cake Addin #addin nuget:?package=Ametrin.Optional&version=0.1.0 // Install Ametrin.Optional as a Cake Tool #tool nuget:?package=Ametrin.Optional&version=0.1.0
Ametrin.Optional
A simple C#/.NET library containing various allocation free option types written by Barion at Ametrin Studios. 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.
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 request.<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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. |
-
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.