-
Notifications
You must be signed in to change notification settings - Fork 2
/
u.go
186 lines (142 loc) · 3.97 KB
/
u.go
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
package u
// True is a `bool` that is set to true
var True = NewBool(true)
// False is a `bool` that is set to false
var False = NewBool(false)
// Bool is a `bool` that can be unset
type Bool = Var[bool]
// NewBool creates a new Bool with the given value
func NewBool(val bool) Bool {
return NewVar(val)
}
// String is a `string` that can be unset
type String = Var[string]
// NewString creates a new String with the given value
func NewString(val string) String {
return NewVar(val)
}
// Float64 is a `float64` that can be unset
type Float64 = Var[float64]
// NewFloat64 creates a new Float64 with the given value
func NewFloat64(val float64) Float64 {
return NewVar(val)
}
// Float32 is a `float32` that can be unset
type Float32 = Var[float32]
// NewFloat32 creates a new Float32 with the given value
func NewFloat32(val float32) Float32 {
return NewVar(val)
}
// Uint is a `uint` that can be unset
type Uint = Var[uint]
// NewUint creates a new Uint with the given value
func NewUint(val uint) Uint {
return NewVar(val)
}
// Uint8 is a `uint8` that can be unset
type Uint8 = Var[uint8]
// NewUint8 creates a new Uint8 with the given value
func NewUint8(val uint8) Uint8 {
return NewVar(val)
}
// Uint16 is a `uint16` that can be unset
type Uint16 = Var[uint16]
// NewUint16 creates a new Uint16 with the given value
func NewUint16(val uint16) Uint16 {
return NewVar(val)
}
// Uint32 is a `uint32` that can be unset
type Uint32 = Var[uint32]
// NewUint32 creates a new Uint32 with the given value
func NewUint32(val uint32) Uint32 {
return NewVar(val)
}
// Uint64 is a `uint64` that can be unset
type Uint64 = Var[uint64]
// NewUint64 creates a new Uint64 with the given value
func NewUint64(val uint64) Uint64 {
return NewVar(val)
}
// Byte is a `byte` that can be unset
type Byte = Var[byte]
// NewByte creates a new Byte with the given value
func NewByte(val byte) Byte {
return NewVar(val)
}
// Rune is a `rune` that can be unset
type Rune = Var[rune]
// NewRune creates a new Rune with the given value
func NewRune(val rune) Rune {
return NewVar(val)
}
// Complex64 is a `complex64` that can be unset
type Complex64 = Var[complex64]
// NewComplex64 creates a new Complex64 with the given value
func NewComplex64(val complex64) Complex64 {
return NewVar(val)
}
// Complex128 is a `complex128` that can be unset
type Complex128 = Var[complex128]
// NewComplex128 creates a new Complex128 with the given value
func NewComplex128(val complex128) Complex128 {
return NewVar(val)
}
// Int is an `int` that can be unset
type Int = Var[int]
// NewInt creates a new Int with the given value
func NewInt(val int) Int {
return NewVar(val)
}
// Int8 is an `int8` that can be unset
type Int8 = Var[int8]
// NewInt8 creates a new Int8 with the given value
func NewInt8(val int8) Int8 {
return NewVar(val)
}
// Int16 is an `int16` that can be unset
type Int16 = Var[int16]
// NewInt16 creates a new Int16 with the given value
func NewInt16(val int16) Int16 {
return NewVar(val)
}
// Int32 is an `int32` that can be unset
type Int32 = Var[int32]
// NewInt32 creates a new Int32 with the given value
func NewInt32(val int32) Int32 {
return NewVar(val)
}
// Int64 is an `int64` that can be unset
type Int64 = Var[int64]
// NewInt64 creates a new Int64 with the given value
func NewInt64(val int64) Int64 {
return NewVar(val)
}
// Var is a variable that can be set, unset and queried for its state.
type Var[T any] struct {
val T
set bool
}
// Get the value of the variable
// Returns the zero value if unset
func (v *Var[T]) Get() T {
return v.val
}
// Set the value of the variable
func (v *Var[T]) Set(val T) {
v.val = val
v.set = true
}
// IsSet returns true when a value has been set
func (v *Var[T]) IsSet() bool {
return v.set
}
// Unset resets the value to the zero value and sets the variable to "unset"
func (v *Var[T]) Unset() {
v.set = false
var temp T
v.val = temp
}
// NewVar creates a new Var with the given value
func NewVar[T any](val T) Var[T] {
return Var[T]{val: val, set: true}
}