@@ -61,13 +61,17 @@ Represents when an actual value might not exist for a value or named variable. A
61
61
62
62
``` csharp
63
63
var option = Option <int >.Some (5 );
64
+
64
65
// or, with no value
65
66
var optionNone = Option <int >.None ();
67
+
68
+ // also returns none
69
+ var optionNull = Option <object >.Some (default ! );
66
70
```
67
71
68
72
### Using Option
69
73
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:
71
75
72
76
``` csharp
73
77
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
134
138
135
139
### Using Results
136
140
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:
138
142
139
143
``` csharp
140
144
public Result < int , string > TryDivide (int numerator , int denominator ) =>
141
145
denominator == 0
142
146
? Result <int , string >.Error (" Cannot divide by zero" )
143
147
: Result <int , string >.Ok (numerator / denominator );
148
+ ```
149
+
150
+ With this method defined we can begin performing operations against the Result result:
144
151
152
+ ``` csharp
145
153
// Exhasutive matching
146
154
TryDivide (10 , 2 )
147
155
.Match (
@@ -191,6 +199,40 @@ Result<int, ResultErrors> resultErrorsTyped = Result<int>.Error(new ResultErrors
191
199
192
200
## ResultOption
193
201
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
+ ```
194
236
195
237
## Contribute
196
238
0 commit comments