-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathif.go
62 lines (50 loc) · 1.25 KB
/
if.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
package gox
import (
"github.com/goexl/gox/internal/callback"
)
var (
_ = If[int]
_ = Iff[int]
_ = Ift[int]
_ = Ifx[int]
)
// If 模块条件表达式,主要用法是减少大括号
func If[T any](condition bool, result T) (t T) {
if condition {
t = result
}
return
}
// Iff 模块条件表达式,主要用法是减少大括号
func Iff[T any](condition bool, result callback.Callback[T]) (t T) {
if condition {
t = result()
}
return
}
// Ift 模拟三元表达式,主要用法是减少大括号
func Ift[T any](condition bool, first T, second T) (t T) {
return Ternary(condition, first, second)
}
// Ternary 模拟三元表达式,主要用法是减少大括号
func Ternary[T any](condition bool, first T, second T) (t T) {
if condition {
t = first
} else {
t = second
}
return
}
// Ifx 模拟三元表达式,主要用法是减少大括号
func Ifx[T any](condition bool, first callback.Callback[T], second callback.Callback[T]) (t T) {
return TernaryFunc(condition, first, second)
}
// TernaryFunc 模拟三元表达式,主要用法是减少大括号
func TernaryFunc[T any](condition bool, first callback.Callback[T], second callback.Callback[T]) (t T) {
if condition {
t = first()
} else {
t = second()
}
return
}