/
Result.cs
694 lines (606 loc) · 28.1 KB
/
Result.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
namespace DotNext;
using Intrinsics = Runtime.Intrinsics;
/// <summary>
/// Represents extension methods for type <see cref="Result{T}"/>.
/// </summary>
public static class Result
{
/// <summary>
/// If a result is successful, returns it, otherwise <see langword="null"/>.
/// </summary>
/// <typeparam name="T">Value type.</typeparam>
/// <param name="result">The result.</param>
/// <returns>Nullable value.</returns>
public static T? OrNull<T>(this in Result<T> result)
where T : struct
=> result.TryGet(out var value) ? value : null;
/// <summary>
/// If a result is successful, returns it, otherwise <see langword="null"/>.
/// </summary>
/// <typeparam name="T">Value type.</typeparam>
/// <typeparam name="TError">The type of the error code.</typeparam>
/// <param name="result">The result.</param>
/// <returns>Nullable value.</returns>
public static T? OrNull<T, TError>(this in Result<T, TError> result)
where T : struct
where TError : struct, Enum
=> result.TryGet(out var value) ? value : null;
/// <summary>
/// Returns the second result if the first is unsuccessful.
/// </summary>
/// <param name="first">The first result.</param>
/// <param name="second">The second result.</param>
/// <typeparam name="T">The type of value.</typeparam>
/// <returns>The second result if the first is unsuccessful; otherwise, the first result.</returns>
public static ref readonly Result<T> Coalesce<T>(this in Result<T> first, in Result<T> second) => ref first.IsSuccessful ? ref first : ref second;
/// <summary>
/// Indicates that specified type is result type.
/// </summary>
/// <param name="resultType">The type of <see cref="Result{T}"/>.</param>
/// <returns><see langword="true"/>, if specified type is result type; otherwise, <see langword="false"/>.</returns>
public static bool IsResult(this Type resultType) => resultType.IsConstructedGenericType && resultType.GetGenericTypeDefinition().IsOneOf([typeof(Result<>), typeof(Result<,>)]);
/// <summary>
/// Returns the underlying type argument of the specified result type.
/// </summary>
/// <param name="resultType">Result type.</param>
/// <returns>Underlying type argument of result type; otherwise, <see langword="null"/>.</returns>
public static Type? GetUnderlyingType(Type resultType) => IsResult(resultType) ? resultType.GetGenericArguments()[0] : null;
/// <summary>
/// Creates a new instance of <see cref="Result{T}"/> from the specified value.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The value to be placed to the container.</param>
/// <returns>The value encapsulated by <see cref="Result{T}"/>.</returns>
public static Result<T> FromValue<T>(T value) => new(value);
/// <summary>
/// Creates a new instance of <see cref="Result{T}"/> from the specified exception.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="e">The exception to be placed to the container.</param>
/// <returns>The exception encapsulated by <see cref="Result{T}"/>.</returns>
public static Result<T> FromException<T>(Exception e) => new(e);
}
/// <summary>
/// Represents a result of operation which can be actual result or exception.
/// </summary>
/// <typeparam name="T">The type of the value stored in the Result monad.</typeparam>
[StructLayout(LayoutKind.Auto)]
public readonly struct Result<T> : IResultMonad<T, Exception, Result<T>>
{
private readonly T value;
private readonly ExceptionDispatchInfo? exception;
/// <summary>
/// Initializes a new successful result.
/// </summary>
/// <param name="value">The value to be stored as result.</param>
public Result(T value)
{
this.value = value;
exception = null;
}
/// <summary>
/// Initializes a new unsuccessful result.
/// </summary>
/// <param name="error">The exception representing error. Cannot be <see langword="null"/>.</param>
public Result(Exception error)
{
exception = ExceptionDispatchInfo.Capture(error);
Unsafe.SkipInit(out value);
}
private Result(ExceptionDispatchInfo dispatchInfo)
{
value = default!;
exception = dispatchInfo;
}
/// <summary>
/// Initializes a new unsuccessful result.
/// </summary>
/// <param name="error">The exception representing error. Cannot be <see langword="null"/>.</param>
/// <returns>The unsuccessful result.</returns>
static Result<T> IResultMonad<T, Exception, Result<T>>.FromError(Exception error) => new(error);
/// <summary>
/// Creates <see cref="Result{T}"/> from <see cref="Optional{T}"/> instance.
/// </summary>
/// <param name="optional">The optional value.</param>
/// <returns>The converted optional value.</returns>
public static Result<T> FromOptional(in Optional<T> optional) => optional switch
{
{ HasValue: true } => new(optional.ValueOrDefault),
{ IsNull: true } => default,
_ => new(new InvalidOperationException(ExceptionMessages.OptionalNoValue))
};
/// <summary>
/// Indicates that the result is successful.
/// </summary>
/// <value><see langword="true"/> if this result is successful; <see langword="false"/> if this result represents exception.</value>
[MemberNotNullWhen(false, nameof(Error))]
public bool IsSuccessful => exception is null;
/// <inheritdoc />
bool IOptionMonad<T>.HasValue => IsSuccessful;
/// <summary>
/// Extracts the actual result.
/// </summary>
/// <exception cref="Exception">This result is not successful.</exception>
public T Value
{
get
{
Validate();
return value;
}
}
/// <summary>
/// Gets a reference to the underlying value.
/// </summary>
/// <value>The reference to the result.</value>
/// <exception cref="Exception">The result is unavailable.</exception>
[UnscopedRef]
[JsonIgnore]
public ref readonly T ValueRef
{
get
{
Validate();
return ref value;
}
}
/// <summary>
/// Gets the value if present; otherwise return default value.
/// </summary>
/// <value>The value, if present, otherwise <c>default</c>.</value>
public T? ValueOrDefault => value;
private void Validate() => exception?.Throw();
/// <inheritdoc />
object? ISupplier<object?>.Invoke() => IsSuccessful ? value : null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Result<TResult> Convert<TResult, TConverter>(TConverter converter)
where TConverter : struct, ISupplier<T, TResult>
{
Result<TResult> result;
if (exception is null)
{
try
{
result = converter.Invoke(value);
}
catch (Exception e)
{
result = new(e);
}
}
else
{
result = new(exception);
}
return result;
}
/// <summary>
/// If successful result is present, apply the provided mapping function hiding any exception
/// caused by the converter.
/// </summary>
/// <param name="converter">A mapping function to be applied to the value, if present.</param>
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam>
/// <returns>The conversion result.</returns>
public Result<TResult> Convert<TResult>(Converter<T, TResult> converter)
=> Convert<TResult, DelegatingConverter<T, TResult>>(converter);
/// <summary>
/// If successful result is present, apply the provided mapping function hiding any exception
/// caused by the converter.
/// </summary>
/// <param name="converter">A mapping function to be applied to the value, if present.</param>
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam>
/// <returns>The conversion result.</returns>
[CLSCompliant(false)]
public unsafe Result<TResult> Convert<TResult>(delegate*<T, TResult> converter)
=> Convert<TResult, Supplier<T, TResult>>(converter);
/// <summary>
/// Attempts to extract value from container if it is present.
/// </summary>
/// <param name="value">Extracted value.</param>
/// <returns><see langword="true"/> if value is present; otherwise, <see langword="false"/>.</returns>
public bool TryGet(out T value)
{
value = this.value;
return exception is null;
}
/// <summary>
/// Returns the value if present; otherwise return default value.
/// </summary>
/// <param name="defaultValue">The value to be returned if this result is unsuccessful.</param>
/// <returns>The value, if present, otherwise <paramref name="defaultValue"/>.</returns>
public T? Or(T? defaultValue) => exception is null ? value : defaultValue;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T OrInvoke<TSupplier>(TSupplier defaultFunc)
where TSupplier : struct, ISupplier<T>
=> exception is null ? value : defaultFunc.Invoke();
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
public T OrInvoke(Func<T> defaultFunc)
=> OrInvoke<DelegatingSupplier<T>>(defaultFunc);
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
[CLSCompliant(false)]
public unsafe T OrInvoke(delegate*<T> defaultFunc)
=> OrInvoke<Supplier<T>>(defaultFunc);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T OrInvokeWithException<TSupplier>(TSupplier defaultFunc)
where TSupplier : struct, ISupplier<Exception, T>
=> exception is null ? value : defaultFunc.Invoke(exception.SourceException);
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
public T OrInvoke(Func<Exception, T> defaultFunc)
=> OrInvokeWithException<DelegatingSupplier<Exception, T>>(defaultFunc);
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
[CLSCompliant(false)]
public unsafe T OrInvoke(delegate*<Exception, T> defaultFunc)
=> OrInvokeWithException<Supplier<Exception, T>>(defaultFunc);
/// <summary>
/// Gets exception associated with this result.
/// </summary>
public Exception? Error => exception?.SourceException;
/// <summary>
/// Returns the value if present; otherwise return default value.
/// </summary>
/// <param name="result">The result to check.</param>
/// <param name="defaultValue">The value to be returned if this result is unsuccessful.</param>
/// <returns>The value, if present, otherwise <paramref name="defaultValue"/>.</returns>
public static T? operator |(in Result<T> result, T? defaultValue)
=> result.Or(defaultValue);
/// <summary>
/// Tries to return successful result.
/// </summary>
/// <param name="x">The first container.</param>
/// <param name="y">The second container.</param>
/// <returns>The first successful result.</returns>
public static Result<T> operator |(in Result<T> x, in Result<T> y)
=> x.IsSuccessful ? x : y;
/// <summary>
/// Gets boxed representation of the result.
/// </summary>
/// <returns>The boxed representation of the result.</returns>
public Result<object?> Box() => exception is null ? new(value) : new(exception);
/// <summary>
/// Extracts actual result.
/// </summary>
/// <param name="result">The result object.</param>
public static explicit operator T(in Result<T> result) => result.Value;
/// <summary>
/// Converts the result into <see cref="Optional{T}"/>.
/// </summary>
/// <returns>Option monad representing value in this monad.</returns>
public Optional<T> TryGet() => exception is null ? new(value) : Optional<T>.None;
/// <summary>
/// Converts the result into <see cref="Optional{T}"/>.
/// </summary>
/// <param name="result">The result to be converted.</param>
/// <returns>Option monad representing value in this monad.</returns>
public static implicit operator Optional<T>(in Result<T> result) => result.TryGet();
/// <summary>
/// Converts value into the result.
/// </summary>
/// <param name="result">The result to be converted.</param>
/// <returns>The result representing <paramref name="result"/> value.</returns>
public static implicit operator Result<T>(T result) => new(result);
/// <summary>
/// Converts <see cref="Optional{T}"/> to <see cref="Result{T}"/>.
/// </summary>
/// <param name="optional">The optional value.</param>
/// <returns>The result representing optional value.</returns>
public static explicit operator Result<T>(in Optional<T> optional) => FromOptional(in optional);
/// <summary>
/// Indicates that both results are successful.
/// </summary>
/// <param name="left">The first result to check.</param>
/// <param name="right">The second result to check.</param>
/// <returns><see langword="true"/> if both results are successful; otherwise, <see langword="false"/>.</returns>
public static bool operator &(in Result<T> left, in Result<T> right) => left.exception is null && right.exception is null;
/// <summary>
/// Indicates that the result is successful.
/// </summary>
/// <param name="result">The result to check.</param>
/// <returns><see langword="true"/> if this result is successful; <see langword="false"/> if this result represents exception.</returns>
public static bool operator true(in Result<T> result) => result.exception is null;
/// <summary>
/// Indicates that the result represents error.
/// </summary>
/// <param name="result">The result to check.</param>
/// <returns><see langword="false"/> if this result is successful; <see langword="true"/> if this result represents exception.</returns>
public static bool operator false(in Result<T> result) => result.exception is not null;
/// <summary>
/// Returns textual representation of this object.
/// </summary>
/// <returns>The textual representation of this object.</returns>
public override string ToString() => exception?.SourceException.ToString() ?? value?.ToString() ?? "<NULL>";
}
/// <summary>
/// Represents a result of operation which can be actual result or error code.
/// </summary>
/// <typeparam name="T">The type of the result.</typeparam>
/// <typeparam name="TError">
/// The type of the error code. Default value must represent the successful result.
/// </typeparam>
[StructLayout(LayoutKind.Auto)]
public readonly struct Result<T, TError> : IResultMonad<T, TError, Result<T, TError>>
where TError : struct, Enum
{
private readonly T value;
private readonly TError errorCode;
/// <summary>
/// Initializes a new successful result.
/// </summary>
/// <param name="value">The result value.</param>
public Result(T value)
{
this.value = value;
}
/// <summary>
/// Initializes a new unsuccessful result.
/// </summary>
/// <param name="error">The error code.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="error"/> represents a successful code.</exception>
public Result(TError error)
{
Unsafe.SkipInit(out value);
errorCode = Intrinsics.IsDefault(in error) ? throw new ArgumentOutOfRangeException(nameof(error)) : error;
}
/// <summary>
/// Initializes a new unsuccessful result.
/// </summary>
/// <param name="error">The error code.</param>
/// <returns>The unsuccessful result.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="error"/> represents a successful code.</exception>
static Result<T, TError> IResultMonad<T, TError, Result<T, TError>>.FromError(TError error) => new(error);
/// <summary>
/// Gets boxed representation of the result.
/// </summary>
/// <returns>The boxed representation of the result.</returns>
public Result<object?, TError> Box() => IsSuccessful ? new(value) : new(errorCode);
/// <summary>
/// Extracts the actual result.
/// </summary>
/// <exception cref="UndefinedResultException{TError}">The value is unavailable.</exception>
public T Value
{
get
{
Validate();
return value;
}
}
/// <summary>
/// Gets a reference to the underlying value.
/// </summary>
/// <value>The reference to the result.</value>
/// <exception cref="UndefinedResultException{TError}">The value is unavailable.</exception>
[UnscopedRef]
[JsonIgnore]
public ref readonly T ValueRef
{
get
{
Validate();
return ref value;
}
}
/// <summary>
/// Returns the value if present; otherwise return default value.
/// </summary>
/// <returns>The value, if present, otherwise <c>default</c>.</returns>
public T? ValueOrDefault => value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Validate()
{
if (!IsSuccessful)
Throw();
}
[StackTraceHidden]
[DoesNotReturn]
private void Throw() => throw new UndefinedResultException<TError>(Error);
/// <inheritdoc />
object? ISupplier<object?>.Invoke() => IsSuccessful ? value : null;
/// <summary>
/// Gets the error code.
/// </summary>
public TError Error => errorCode;
/// <summary>
/// Indicates that the result is successful.
/// </summary>
/// <value><see langword="true"/> if this result is successful; <see langword="false"/> if this result represents exception.</value>
public bool IsSuccessful => Intrinsics.IsDefault(in errorCode);
/// <inheritdoc />
bool IOptionMonad<T>.HasValue => IsSuccessful;
/// <summary>
/// Attempts to extract value from container if it is present.
/// </summary>
/// <param name="value">Extracted value.</param>
/// <returns><see langword="true"/> if value is present; otherwise, <see langword="false"/>.</returns>
public bool TryGet(out T value)
{
value = this.value;
return IsSuccessful;
}
/// <summary>
/// Converts the result into <see cref="Optional{T}"/>.
/// </summary>
/// <returns>Option monad representing value in this monad.</returns>
public Optional<T> TryGet() => IsSuccessful ? new(value) : Optional<T>.None;
/// <summary>
/// Returns the value if present; otherwise return default value.
/// </summary>
/// <param name="defaultValue">The value to be returned if this result is unsuccessful.</param>
/// <returns>The value, if present, otherwise <paramref name="defaultValue"/>.</returns>
public T? Or(T? defaultValue) => IsSuccessful ? value : defaultValue;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Result<TResult, TError> Convert<TResult, TConverter>(TConverter converter)
where TConverter : struct, ISupplier<T, TResult>
=> IsSuccessful ? new(converter.Invoke(value)) : new(Error);
/// <summary>
/// If successful result is present, apply the provided mapping function hiding any exception
/// caused by the converter.
/// </summary>
/// <param name="converter">A mapping function to be applied to the value, if present.</param>
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam>
/// <returns>The conversion result.</returns>
public Result<TResult, TError> Convert<TResult>(Converter<T, TResult> converter)
=> Convert<TResult, DelegatingConverter<T, TResult>>(converter);
/// <summary>
/// If successful result is present, apply the provided mapping function hiding any exception
/// caused by the converter.
/// </summary>
/// <param name="converter">A mapping function to be applied to the value, if present.</param>
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam>
/// <returns>The conversion result.</returns>
[CLSCompliant(false)]
public unsafe Result<TResult, TError> Convert<TResult>(delegate*<T, TResult> converter)
=> Convert<TResult, Supplier<T, TResult>>(converter);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T OrInvoke<TSupplier>(TSupplier defaultFunc)
where TSupplier : struct, ISupplier<T>
=> IsSuccessful ? value : defaultFunc.Invoke();
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
public T OrInvoke(Func<T> defaultFunc)
=> OrInvoke<DelegatingSupplier<T>>(defaultFunc);
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
[CLSCompliant(false)]
public unsafe T OrInvoke(delegate*<T> defaultFunc)
=> OrInvoke<Supplier<T>>(defaultFunc);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T OrInvokeWithError<TSupplier>(TSupplier defaultFunc)
where TSupplier : struct, ISupplier<TError, T>
=> IsSuccessful ? value : defaultFunc.Invoke(Error);
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
public T OrInvoke(Func<TError, T> defaultFunc)
=> OrInvokeWithError<DelegatingSupplier<TError, T>>(defaultFunc);
/// <summary>
/// Returns the value if present; otherwise invoke delegate.
/// </summary>
/// <param name="defaultFunc">A delegate to be invoked if value is not present.</param>
/// <returns>The value, if present, otherwise returned from delegate.</returns>
[CLSCompliant(false)]
public unsafe T OrInvoke(delegate*<TError, T> defaultFunc)
=> OrInvokeWithError<Supplier<TError, T>>(defaultFunc);
private T OrThrow<TExceptionFactory>(TExceptionFactory factory)
where TExceptionFactory : struct, ISupplier<TError, Exception>
=> IsSuccessful ? value : Throw(factory);
[DoesNotReturn]
[StackTraceHidden]
private T Throw<TExceptionFactory>(TExceptionFactory factory)
where TExceptionFactory : struct, ISupplier<TError, Exception>
=> throw factory.Invoke(Error);
/// <summary>
/// Gets underlying value or throws an exception.
/// </summary>
/// <param name="exceptionFactory">The exception factory that accepts the error code.</param>
/// <returns>The underlying value.</returns>
/// <exception cref="Exception">The result is unsuccessful.</exception>
public T OrThrow(Func<TError, Exception> exceptionFactory)
=> OrThrow<DelegatingSupplier<TError, Exception>>(exceptionFactory);
/// <summary>
/// Gets underlying value or throws an exception.
/// </summary>
/// <param name="exceptionFactory">The exception factory that accepts the error code.</param>
/// <returns>The underlying value.</returns>
/// <exception cref="Exception">The result is unsuccessful.</exception>
[CLSCompliant(false)]
public unsafe T OrThrow(delegate*<TError, Exception> exceptionFactory)
=> OrThrow<Supplier<TError, Exception>>(exceptionFactory);
/// <summary>
/// Converts this result into <see cref="Result{T}"/>.
/// </summary>
/// <returns>The converted result.</returns>
public Result<T> ToResult() => IsSuccessful ? new(value) : new(new UndefinedResultException<TError>(Error));
/// <summary>
/// Returns textual representation of this object.
/// </summary>
/// <returns>The textual representation of this object.</returns>
public override string? ToString() => IsSuccessful ? value?.ToString() : errorCode.ToString();
/// <summary>
/// Returns the value if present; otherwise return default value.
/// </summary>
/// <param name="result">The result to check.</param>
/// <param name="defaultValue">The value to be returned if this result is unsuccessful.</param>
/// <returns>The value, if present, otherwise <paramref name="defaultValue"/>.</returns>
public static T? operator |(in Result<T, TError> result, T? defaultValue)
=> result.Or(defaultValue);
/// <summary>
/// Tries to return successful result.
/// </summary>
/// <param name="x">The first container.</param>
/// <param name="y">The second container.</param>
/// <returns>The first successful result.</returns>
public static Result<T, TError> operator |(in Result<T, TError> x, in Result<T, TError> y)
=> x.IsSuccessful ? x : y;
/// <summary>
/// Converts the result into <see cref="Optional{T}"/>.
/// </summary>
/// <param name="result">The result to be converted.</param>
/// <returns>Option monad representing value in this monad.</returns>
public static implicit operator Optional<T>(in Result<T, TError> result) => result.TryGet();
/// <summary>
/// Converts the result into <see cref="Result{T}"/>.
/// </summary>
/// <param name="result">The result to be converted.</param>
/// <returns>The converted result.</returns>
public static implicit operator Result<T>(in Result<T, TError> result) => result.ToResult();
/// <summary>
/// Converts value into the result.
/// </summary>
/// <param name="result">The result to be converted.</param>
/// <returns>The result representing <paramref name="result"/> value.</returns>
public static implicit operator Result<T, TError>(T result) => new(result);
/// <summary>
/// Extracts actual result.
/// </summary>
/// <param name="result">The result object.</param>
public static explicit operator T(in Result<T, TError> result) => result.Value;
/// <summary>
/// Indicates that both results are successful.
/// </summary>
/// <param name="left">The first result to check.</param>
/// <param name="right">The second result to check.</param>
/// <returns><see langword="true"/> if both results are successful; otherwise, <see langword="false"/>.</returns>
public static bool operator &(in Result<T, TError> left, in Result<T, TError> right) => left.IsSuccessful && right.IsSuccessful;
/// <summary>
/// Indicates that the result is successful.
/// </summary>
/// <param name="result">The result to check.</param>
/// <returns><see langword="true"/> if this result is successful; <see langword="false"/> if this result represents exception.</returns>
public static bool operator true(in Result<T, TError> result) => result.IsSuccessful;
/// <summary>
/// Indicates that the result represents error.
/// </summary>
/// <param name="result">The result to check.</param>
/// <returns><see langword="false"/> if this result is successful; <see langword="true"/> if this result represents exception.</returns>
public static bool operator false(in Result<T, TError> result) => !result.IsSuccessful;
}