You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`BV`|`bool`| Direct value | Zero value of type `T`| Returns the first value if the condition is true, otherwise returns the zero value of type `T`. |
114
+
|`BF`|`bool`| Function returning value | Zero value of type `T`| Evaluates the function only if the condition is true, otherwise returns the zero value of type `T`. |
115
+
|`FV`|`func() bool`| Direct value | Zero value of type `T`| Evaluates the condition function and returns the first value if true, otherwise returns the zero value of type `T`. |
116
+
|`FF`|`func() bool`| Function returning value | Zero value of type `T`| Evaluates the condition function and returns the result of the first function if true, otherwise returns the zero value of type `T`. |
117
+
118
+
---
119
+
120
+
### Examples
121
+
122
+
#### Using `BV`
123
+
124
+
```go
125
+
result:= tern.BV(false, "value")
126
+
// Output: (empty string, as the condition is false)
// Output: (empty string, as the condition function returns false)
150
+
```
151
+
152
+
---
153
+
154
+
### Additional Utility Functions in `terngo` Package
155
+
156
+
The `terngo` subpackage extends the functionality of `tern` with additional utility functions, specifically designed for comparing values to their zero value. These functions allow for more expressive handling of default values when one of the inputs might be zero.
|`VV`| Direct comparison | Direct value | Direct value | Returns the first value if it is not the zero value of type `T`, otherwise returns the second value. |
161
+
|`VF`| Direct comparison | Direct value | Function returning value | Returns the first value if it is not the zero value of type `T`, otherwise evaluates and returns the result of the fallback function. |
162
+
163
+
---
164
+
165
+
### Examples
166
+
167
+
#### Using `VV`
168
+
169
+
```go
170
+
package main
171
+
172
+
import (
173
+
"fmt"
174
+
"github.com/yyle88/tern/terngo"
175
+
)
176
+
177
+
funcmain() {
178
+
result:= terngo.VV("non-zero", "fallback")
179
+
fmt.Println(result) // Output: "non-zero"
180
+
181
+
result = terngo.VV("", "fallback")
182
+
fmt.Println(result) // Output: "fallback"
183
+
}
184
+
```
185
+
186
+
#### Using `VF`
187
+
188
+
```go
189
+
package main
190
+
191
+
import (
192
+
"fmt"
193
+
"github.com/yyle88/tern/terngo"
194
+
)
195
+
196
+
funcmain() {
197
+
fallbackFunc:=func() string { return"fallback from func" }
198
+
199
+
result:= terngo.VF("non-zero", fallbackFunc)
200
+
fmt.Println(result) // Output: "non-zero"
201
+
202
+
result = terngo.VF("", fallbackFunc)
203
+
fmt.Println(result) // Output: "fallback from func"
204
+
}
205
+
```
206
+
207
+
---
208
+
209
+
### Key Benefits of `VV` and `VF`
210
+
211
+
-**Zero-Value Handling**: These functions leverage `tern.Zero[T]()` to check if the primary value is a zero value.
212
+
-**Fallback Options**: `VF` provides flexibility by allowing dynamic evaluation of the fallback value through a function.
213
+
214
+
---
215
+
216
+
By including these functions in the `terngo` package, developers gain even more expressive tools to simplify conditional logic in Go.
217
+
218
+
### Additional Utility Functions for Pointer Modification in `terngo`
219
+
220
+
The `terngo` subpackage introduces `PV` and `PF`, two specialized functions for safely modifying a pointer's value when it holds the zero value of its type. These functions ensure safer and more expressive manipulation of pointers while providing fallback options.
|`PV`| Pointer to direct value | Direct value | Sets the pointed-to value to the fallback value if it is the zero value of type `T`. Panics if the pointer is `nil`. |
225
+
|`PF`| Pointer to direct value | Function returning value | Sets the pointed-to value to the result of the fallback function if it is the zero value of type `T`. Panics if the pointer is `nil`. |
226
+
227
+
---
228
+
229
+
### Examples
230
+
231
+
#### Using `PV`
232
+
233
+
```go
234
+
package main
235
+
236
+
import (
237
+
"fmt"
238
+
"github.com/yyle88/tern/terngo"
239
+
)
240
+
241
+
funcmain() {
242
+
varvalueint
243
+
terngo.PV(&value, 42)
244
+
fmt.Println(value) // Output: 42
245
+
246
+
value = 7
247
+
terngo.PV(&value, 99)
248
+
fmt.Println(value) // Output: 7
249
+
}
250
+
```
251
+
252
+
#### Using `PF`
253
+
254
+
```go
255
+
package main
256
+
257
+
import (
258
+
"fmt"
259
+
"github.com/yyle88/tern/terngo"
260
+
)
261
+
262
+
funcmain() {
263
+
varvalueint
264
+
fallbackFunc:=func() int { return42 }
265
+
266
+
terngo.PF(&value, fallbackFunc)
267
+
fmt.Println(value) // Output: 42
268
+
269
+
value = 7
270
+
terngo.PF(&value, fallbackFunc)
271
+
fmt.Println(value) // Output: 7
272
+
}
273
+
```
274
+
275
+
---
276
+
277
+
### Key Features of `PV` and `PF`
278
+
279
+
-**Pointer Safety**: These functions panic if the provided pointer is `nil`, ensuring reliable execution.
280
+
-**Zero-Value Check**: Use `tern.Zero[T]()` to determine whether the current value is the zero value of its type.
281
+
-**Dynamic Fallback**: `PF` allows for fallback values to be generated dynamically via a function.
282
+
283
+
By leveraging `PV` and `PF`, developers can seamlessly handle zero-value assignments for pointer-based logic, maintaining both clarity and robustness in their Go code.
284
+
107
285
## Why Use `tern`?
108
286
109
287
1.**Readability**: Simplifies conditional logic, making the code easier to follow.
0 commit comments