forked from AndreyTsvetkov/Functional.Maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaybeReturns.cs
73 lines (68 loc) · 1.92 KB
/
MaybeReturns.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
using System;
namespace Functional.Maybe
{
/// <summary>
/// Fluent exts for unwrapping values from the Maybe
/// </summary>
public static class MaybeReturns
{
/// <summary>
/// Returns <paramref name="a"/>.Value.ToString() or <paramref name="default"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <param name="default"></param>
/// <returns></returns>
public static string ReturnToString<T>(this Maybe<T> a, string @default)
{
return a.HasValue ? a.Value.ToString() : @default;
}
/// <summary>
/// Returns <paramref name="a"/>.Value or throws <paramref name="e"/>()
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <param name="e"></param>
/// <returns></returns>
public static T OrElse<T>(this Maybe<T> a, Func<Exception> e)
{
if (a.IsNothing())
{
throw e();
}
return a.Value;
}
/// <summary>
/// Returns <paramref name="a"/>.Value or returns <paramref name="default"/>()
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <param name="default"></param>
/// <returns></returns>
public static T OrElse<T>(this Maybe<T> a, Func<T> @default)
{
return a.HasValue ? a.Value : @default();
}
/// <summary>
/// Returns <paramref name="a"/>.Value or returns default(<typeparamref name="T"/>)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <returns></returns>
public static T OrElseDefault<T>(this Maybe<T> a)
{
return a.HasValue ? a.Value : default(T);
}
/// <summary>
/// Returns <paramref name="a"/>.Value or returns <paramref name="default"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <param name="default"></param>
/// <returns></returns>
public static T OrElse<T>(this Maybe<T> a, T @default)
{
return a.HasValue ? a.Value : @default;
}
}
}