This commit is contained in:
50
README.md
50
README.md
@@ -1,2 +1,50 @@
|
||||
# MaybeError
|
||||
A simple library to allow "error as values" functionallity in C#
|
||||
A simple library to allow "error as values" functionallity in C# inspired by the Result enum from rust.
|
||||
|
||||
|
||||
# Usage
|
||||
|
||||
All you need to do is wrap you return in `Maybe` or `ValueMaybe` and you can leave most of your code unchanced. Return your normal types as is. When you have an error you can either return a caught exception or create a return a new error like below.
|
||||
|
||||
```csharp
|
||||
|
||||
public Maybe<string> SomeFunction(bool isGood)
|
||||
{
|
||||
if (isGood)
|
||||
return "All good";
|
||||
return new Error("This is not good");
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Once you have a `Maybe<T>` you can inspect it's contents in several ways.
|
||||
|
||||
```csharp
|
||||
|
||||
var result = SomeFunction(true);
|
||||
|
||||
|
||||
// Get the value with an optional default value if there is an error
|
||||
var message = result.ValueOrDefault("Not good, but it's ok");
|
||||
|
||||
|
||||
//The value is implicitly where possible, but will throw the error as an exception if there was one
|
||||
string messageCast = result;
|
||||
|
||||
// You can also check if there was and error manually
|
||||
if (result.HasError)
|
||||
{
|
||||
// do something
|
||||
}
|
||||
|
||||
// Or for a value if you prefer
|
||||
if (result.HasValue)
|
||||
{
|
||||
// do something
|
||||
}
|
||||
|
||||
// You can access the error like this
|
||||
if (result.HasError)
|
||||
Console.WriteLine(result.Error);
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user