-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSafe.cs
More file actions
29 lines (25 loc) · 724 Bytes
/
Safe.cs
File metadata and controls
29 lines (25 loc) · 724 Bytes
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
namespace SER.Code.Helpers;
/// <summary>
/// Wrapper for a value that might be read before it's set.
/// </summary>
public struct Safe<T>
{
private readonly bool _set;
public T Value
{
get
{
if (!_set)
throw new InvalidOperationException($"Attempted to get {typeof(T).Name} before it was set.");
return field;
}
private init
{
field = value;
_set = true;
}
}
public static implicit operator T(Safe<T> wrapper) => wrapper.Value;
public static implicit operator Safe<T>(T value) => new() { Value = value };
public override string ToString() => Value?.ToString() ?? "null";
}