-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.go
98 lines (84 loc) · 2.04 KB
/
panic.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
package gost
// Panics the current thread.
//
// gost.Panic("This is a panic")
func Panic(message String, args ...any) {
panic(Format(message, args...))
}
// Asserts that a boolean expression is true at runtime.
//
// gost.Assert(true, "This is true")
func Assert(condition Bool, args ...any) {
if !condition {
if len(args) > 0 {
if message, ok := args[0].(String); ok {
panic(Format(message, args...))
}
}
panic(Format("assertion failed"))
}
}
// Asserts that two expressions are equal to each other
//
// gost.AssertEq(1, 1, "These are equal")
func AssertEq[T Eq[T]](lhs T, rhs T, args ...any) {
if !lhs.Eq(rhs) {
panicMessage := Format("assertion failed: {} != {}", lhs, rhs)
if len(args) > 0 {
if message, ok := args[0].(String); ok {
panicMessage += "\n>>> " + Format(message, args...)
}
if message, ok := args[0].(string); ok {
panicMessage += "\n>>> " + Format(String(message), args...)
}
}
panic(panicMessage)
}
}
// Asserts that two expressions are not equal to each other
//
// gost.AssertNe(1, 2, "These are not equal")
func AssertNe[T Eq[T]](lhs T, rhs T, args ...any) {
if lhs.Eq(rhs) {
panicMessage := Format("assertion failed: {} == {}", lhs, rhs)
if len(args) > 0 {
if message, ok := args[0].(String); ok {
panicMessage += "\n>>> " + Format(message, args...)
}
if message, ok := args[0].(string); ok {
panicMessage += "\n>>> " + Format(String(message), args...)
}
}
panic(panicMessage)
}
}
// Indicates unimplemented code by panicking with a message of “not implemented”.
//
// gost.Unimplemented()
func Unimplemented(messages ...String) {
if len(messages) == 0 {
panic("not implemented")
} else {
panic(messages[0])
}
}
// Indicates unreachable code.
//
// gost.Unreachable()
func Unreachable(messages ...String) {
if len(messages) == 0 {
panic("unreachable")
} else {
panic(messages[0])
}
}
// Indicates unfinished code.
//
// gost.Todo()
func Todo(messages ...String) {
if len(messages) == 0 {
panic("todo")
} else {
panic(messages[0])
}
}