-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaybeDemo.cs
78 lines (63 loc) · 2.2 KB
/
MaybeDemo.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
using System;
using System.Reflection.Emit;
using InCube.Core.Functional;
using static InCube.Core.Demo.Functional.OptionDemo;
#pragma warning disable SA1400 // Access modifier should be declared
namespace InCube.Core.Demo.Functional
{
internal static class MaybeDemo
{
internal static void Run()
{
Print(nameof(MemoryOptimizationForReferenceTypes));
MemoryOptimizationForReferenceTypes();
Print();
}
private static int SizeOf<T>()
{
var dm = new DynamicMethod("SizeOfType", typeof(int), Array.Empty<Type>());
var il = dm.GetILGenerator();
il.Emit(OpCodes.Sizeof, typeof(T));
il.Emit(OpCodes.Ret);
return (int)dm.Invoke(null, null);
}
private static void MemoryOptimizationForReferenceTypes()
{
Print(SizeOf<int>());
Print(SizeOf<int?>());
Print(SizeOf<Option<int>>());
Print(SizeOf<double>());
Print(SizeOf<double?>());
Print(SizeOf<Option<double>>());
Print(SizeOf<string>());
Print(SizeOf<Option<string>>());
Print(SizeOf<Maybe<string>>());
/* Output
4
8
8
8
16
16
8
16
8
*/
}
private readonly struct Maybe<T>
where T : class
{
private readonly T value;
private Maybe(T value) => this.value = value;
public static readonly Maybe<T> None = default;
public static implicit operator Maybe<T>(T value) => new(value);
public static explicit operator T(Maybe<T> maybe) => maybe.Value;
public bool HasValue => !ReferenceEquals(this.value, null);
public T Value => this.value ?? throw new InvalidOperationException("Nullable object must have a value");
public T GetValueOrDefault() => this.value;
public T GetValueOrDefault(T @default) => this.value ?? @default;
public T GetValueOr(Func<T> @default) => this.value ?? @default.Invoke();
// ...
}
}
}