-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.go
49 lines (40 loc) · 924 Bytes
/
syntax.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
package gost
type ifElse[T any] struct {
condition Bool
expression func() T
}
type ifElseContext[T any] struct {
returnValue *T
}
// If-else statement.
//
// n := I32(5)
// foo := If(n == 10, func() String {
// return "This is 10"
// }).ElseIf(n == 5, func() String {
// return "This is 5"
// }).Else(func() String {
// return "This is not 5 or 10"
// })
// AssertEq(foo, String("This is 5"))
func If[T any](condition Bool, expression func() T) ifElseContext[T] {
context := ifElseContext[T]{}
if condition {
value := expression()
context.returnValue = &value
}
return context
}
func (self ifElseContext[T]) Else(expression func() T) T {
if self.returnValue != nil {
return *self.returnValue
}
return expression()
}
func (self ifElseContext[T]) ElseIf(condition Bool, expression func() T) ifElseContext[T] {
if condition {
value := expression()
self.returnValue = &value
}
return self
}