Skip to content

Commit 7bfd9f0

Browse files
committed
readme
1 parent 634e771 commit 7bfd9f0

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

README.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ Represents when an actual value might not exist for a value or named variable. A
6161

6262
```csharp
6363
var option = Option<int>.Some(5);
64+
6465
// or, with no value
6566
var optionNone = Option<int>.None();
67+
68+
// also returns none
69+
var optionNull = Option<object>.Some(default!);
6670
```
6771

6872
### Using Option
6973

70-
Options are commonly used when a operation might not return a value.
74+
Options are commonly used when a operation might not return a value. For example:
7175

7276
```csharp
7377
public Option<int> TryFind(IEnumerable<int> numbers, Func<int, bool> predicate) =>
@@ -134,14 +138,18 @@ var resultErrorsTyped = Result<int>.Error(new ResultErrors("error-key", "An erro
134138

135139
### Using Results
136140

137-
Results are commonly used when an operation might not succeed, and you want to manage the _expected_ errors.
141+
Results are commonly used when an operation might not succeed, and you want to manage the _expected_ errors. For example:
138142

139143
```csharp
140144
public Result<int, string> TryDivide(int numerator, int denominator) =>
141145
denominator == 0
142146
? Result<int, string>.Error("Cannot divide by zero")
143147
: Result<int, string>.Ok(numerator / denominator);
148+
```
149+
150+
With this method defined we can begin performing operations against the Result result:
144151

152+
```csharp
145153
// Exhasutive matching
146154
TryDivide(10, 2)
147155
.Match(
@@ -191,6 +199,40 @@ Result<int, ResultErrors> resultErrorsTyped = Result<int>.Error(new ResultErrors
191199

192200
## ResultOption
193201

202+
Represents a combination of the Result and Option monads. This is useful when you want to handle both the success and failure of an operation, but also want to handle the case where a value might not exist. It simplifies the inspection by eliminating the redundant nested `Match` calls.
203+
204+
### Creating ResultOptions
205+
206+
```csharp
207+
var resultOption = ResultOption<int, string>.Ok(5);
208+
// or, with an error
209+
var resultOptionError = ResultOption<int, string>.Error("An error occurred");
210+
// or, with no value
211+
var resultOptionNone = ResultOption<int, string>.None();
212+
```
213+
214+
### Using ResultOptions
215+
216+
ResultOptions are commonly used when an operation might not succeed, but also where a value might not exist. For example:
217+
218+
```csharp
219+
public Option<int> LookupUserId(string username) => // ...
220+
221+
public ResultOption<int, string> GetUserId(string username)
222+
{
223+
if(username == "admin")
224+
{
225+
return ResultOption<int,string>.Error("Invalid username");
226+
}
227+
228+
return LookupUserId(username).Match(
229+
some: id => ResultOption<int, string>.Ok(1) :
230+
none: ResultOption<int, string>.None);
231+
232+
// or, using the extension method
233+
LookupUserId(username).ToResultOption();
234+
}
235+
```
194236

195237
## Contribute
196238

0 commit comments

Comments
 (0)