diff --git a/Funzo.Serialization/Funzo.Serialization.csproj b/Funzo.Serialization/Funzo.Serialization.csproj
index d33e007..3be36f8 100644
--- a/Funzo.Serialization/Funzo.Serialization.csproj
+++ b/Funzo.Serialization/Funzo.Serialization.csproj
@@ -16,7 +16,6 @@
$(AssemblyVersion)$(AssemblyVersion)LICENSE
- True
diff --git a/Funzo.SourceGenerators/Funzo.SourceGenerators.csproj b/Funzo.SourceGenerators/Funzo.SourceGenerators.csproj
index 5edeb65..c03d77b 100644
--- a/Funzo.SourceGenerators/Funzo.SourceGenerators.csproj
+++ b/Funzo.SourceGenerators/Funzo.SourceGenerators.csproj
@@ -17,7 +17,6 @@
$(AssemblyVersion)$(AssemblyVersion)LICENSE
- True
diff --git a/Funzo/Funzo.csproj b/Funzo/Funzo.csproj
index d6aebff..aca61b5 100644
--- a/Funzo/Funzo.csproj
+++ b/Funzo/Funzo.csproj
@@ -16,7 +16,6 @@
$(AssemblyVersion)$(AssemblyVersion)LICENSE
- True
diff --git a/Funzo/IResult.cs b/Funzo/IResultBase.cs
similarity index 100%
rename from Funzo/IResult.cs
rename to Funzo/IResultBase.cs
diff --git a/Funzo/Result.cs b/Funzo/Result.cs
index 3c87243..14e53b8 100644
--- a/Funzo/Result.cs
+++ b/Funzo/Result.cs
@@ -29,6 +29,17 @@ private Result(TErr err) : base(err)
/// An instance of as a failed operation
public static Result Err(TErr err) => new(err);
+ ///
+ /// Converts into implicitly
+ ///
+ /// Value
+ public static implicit operator Result(TOk ok) => new(ok);
+ ///
+ /// Converts into implicitly
+ ///
+ /// Value
+ public static implicit operator Result(TErr err) => new(err);
+
///
public override bool Equals(object? obj)
{
@@ -78,6 +89,12 @@ private Result(TErr err) : base(err)
/// An instance of as a failed operation
public static Result Err(TErr err) => new(err);
+ ///
+ /// Converts into implicitly
+ ///
+ /// Value
+ public static implicit operator Result(TErr err) => new(err);
+
///
public override bool Equals(object? obj)
{
diff --git a/README.md b/README.md
index 3b74889..fa539d3 100644
--- a/README.md
+++ b/README.md
@@ -5,15 +5,20 @@
# Funzo
## Contents
+
- [Where is this coming from](#where-is-this-coming-from)
- [Description](#description)
+- [Comparison with other packages](#comparison-with-other-packages)
+ - [OneOf](#one-of)
+ - [CSharpFunctionalExtensions](#csharpfunctionalextensions)
+ - [FluentResults](#fluentresults)
- [Usage (recommended way)](#usage-recommended-way)
- [Documentation](#documentation)
- - [Unit](#unit)
- - [Option](#option)
- - [Result](#result)
- - [ResultBuilder](#resultbuilder)
- - [Union](#union)
+ - [Unit](#unit)
+ - [Option](#option)
+ - [Result](#result)
+ - [ResultBuilder](#resultbuilder)
+ - [Union](#union)
- [Serialization](#serialization)
- [Example](#example)
- [Further work](#further-work)
@@ -21,10 +26,12 @@
- [What's missing (updated after adding union types & source generators)](#whats-missing-updated)
## Where is this coming from
+
This package was previously called OptionTypes, but given that now more things are being added, I think a new name could fit better. Wanted to make it sound like something functional, so Funzo it is. Sounds fine.
Apart from the name, the `Maybe` class was renamed to `Option`. It is more common than `Maybe` so I thought it would be better for people to identify it. I ~~copied a lot of things~~ took inspiration of the work done by [`OneOf`](https://github.com/mcintyre321/OneOf) (specially source generators) but decided to adapt it to my way of doing things: **making everything explicit**.
## Description
+
_Funzo_ allows the developer to use some classes more commonly used in functional programming for error-proof programming and better type definition. It contains 4 classes:
- The `Unit` class represents an empty class. Because functions always return something, `Unit` is the equivalent to `void`
@@ -37,8 +44,28 @@ _Funzo_ allows the developer to use some classes more commonly used in functiona
- The attributes `ResultAttribute` and `UnionAttribute` allow you to generate types and use them with ease.
+## Comparison with other packages
+
+Here is a small comparison with other popular libraries. Each column identifies if the library has what's described and to what extent.
+
+- **Result**: A result type with 2 options: OK and ERR.
+- **Option**: An option type that can show the absence of value without null.
+- **Union**: Allows to create discriminated unions.
+- **Source generator**: Has a source generator to create your classes with ease.
+- **Type safety**: Exposes methods that can throw exceptions.
+
+| Library | Result | Option | Union | Source Generator | Type Safety |
+| --------------------------------------------------------------------------------------- | ---------------------------------- | ------------------------------------ | ----- | ---------------- | ---------------------------------------------------- |
+| [`OneOf`](https://github.com/mcintyre321/OneOf) | NO, but can be created with unions | NO, but can be created with unions | YES | YES | YES |
+| [`CSharpFunctionalExtensions`](https://github.com/vkhorikov/CSharpFunctionalExtensions) | YES. Dedicated types | YES. Dedicated types | NO | NO | MIXED. You can skip type safety and get exceptions |
+| [`FluentResults`](https://github.com/altmann/FluentResults) | YES. Dedicated types | NO, but can be created with a Result | NO | NO | MIXED. You can skip type checking and get exceptions |
+| [`ErrorOr`](https://github.com/amantinband/error-or) | YES. Dedicated types | NO, but can be created with a Result | NO | NO | MIXED. You can skip type safety and get exceptions |
+| Funzo | YES. Dedicated types | YES. Dedicated types | YES | YES | YES |
+
+Please understand that this comparison is done on a feature basis and each library has good things over the others. Just because Funzo has 5 YES does not mean that it is the best. It fulfills everything because it was created with just this 5 things in mind.
## Usage (recommended way)
+
The best way to approach this package is through source generators. This will allow you to define your types with ease and work on your problems straight away.
You will need the `Funzo` and `Funzo.SourceGenerators` packages.
For unions, decorate it with the `Union` attribute, supplying the generic parameters you want as possible options.
@@ -59,15 +86,16 @@ public partial class ItemCreationError;
public partial class ItemCreatedResult;
```
-
## Documentation
### Unit
+
Unit is a helper type to represent the absence of a return value (think of it as void). Because in functional programming every function returns a value, it is added here for compatibility.
### Option
Create a new Option using one of the helper methods (`Option.FromValue`, `Option.Some`, and `Option.None`):
+
```Csharp
var optionInt = Option.Some(1);
var optionFloat = Option.FromValue(12);
@@ -76,6 +104,7 @@ var optionString = Option.FromValue(nullableString);
```
Map its content using the `Map` method:
+
```Csharp
var optionText = await ReadTextFromFile(filePath);
@@ -83,6 +112,7 @@ var uppercaseText = optionText.Map(text => text.ToUpper());
```
You can also map to another `Option` and it will be flatten:
+
```Csharp
var number = Option.FromValue(1);
@@ -91,6 +121,7 @@ var double = number.Map(x => Option.FromValue(x * 2));
```
If you want to check both options, use the `Match` method:
+
```Csharp
let user = await GetUser();
@@ -99,12 +130,14 @@ var userName = user.Match(x => x.Name, () => "User not found");
```
In case you want to provide a fallback value, you can use `ValueOr`:
+
```Csharp
var optionUserName = await GetUserName();
var userName = optionUserName.ValueOr("Unknown user");
```
In case you want to do something if there is a value present, you can use the `IsSome` method:
+
```Csharp
Option optionUser = await GetUser();
@@ -116,17 +149,8 @@ if(!optionUser.IsSome(out User user))
var posts = postService.GetPostsByUserId(user.Id);
```
-You can force the value out using the `Unwrap` method. This approach is **not recommended**:
-```Csharp
-var optionValue = Option.Some(1);
-
-var value = optionValue.Unwrap() // 1
+There are also extension methods for `Task