Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BarionLP committed Nov 14, 2024
2 parents ff249fc + c643151 commit c7ef546
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 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](https://github.com/BarionLP/Ametrin.Utils).
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.
This is a rewrite of the Optional namespace in [Ametrin.Utils](https://github.com/BarionLP/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 None
T or Error
```csharp
Option<T> a = someT; // equivalent to Option.Of(someT)
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
Expand All @@ -22,8 +22,8 @@ Option error = false; //equivalent to Option.Error()
## Result\<T, E>
T or E
```csharp
Result<T, E> success = someT; // equivalent to Option.Success<T>()
Result<T, E> error = someE; // equivalent to Option.Error<T>()
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
Expand All @@ -34,12 +34,12 @@ Success or Exception

## General API
All option types in this library have a monadic, linq-like api to interact with them.
### Examples:
### Examples
```csharp
option.Select(value => value.ToString()).WhereNot(string.IsNullOfWhiteSpace).Or("John Doe");
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 a `...OrNone` alternative most `Try...` methods. If not, I'm happy to accept pull request.
Not all functions might exist for every option type. Feel free to PR them.
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)

0 comments on commit c7ef546

Please sign in to comment.