-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.go
43 lines (36 loc) · 959 Bytes
/
util.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
package tuple
import (
"fmt"
"reflect"
"strings"
)
// typeName returns the name of the type parameters.
func typeName[T any]() string {
var val T
ty := reflect.TypeOf(&val).Elem()
return ty.String()
}
// tupString returns a string representation of the tuple values.
func tupString(values []any) string {
valuesStr := make([]string, len(values))
for i, val := range values {
valuesStr[i] = fmt.Sprintf("%#v", val)
}
return "[" + strings.Join(valuesStr, " ") + "]"
}
// tupGoString returns a Go-syntax representation of a tuple holding the given values.
func tupGoString(values []any) string {
types := make([]string, len(values))
for i, val := range values {
types[i] = fmt.Sprintf("%T", val)
}
fields := make([]string, len(values))
for i, val := range values {
fields[i] = fmt.Sprintf("V%d: %#v", i+1, val)
}
return fmt.Sprintf("tuple.T%d[%s]{%s}",
len(values),
strings.Join(types, ", "),
strings.Join(fields, ", "),
)
}