-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.go
54 lines (46 loc) · 1.28 KB
/
names.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
package asyncpi
import (
"go.nickng.io/asyncpi/internal/name"
)
// newNames is a convenient utility function
// for creating a []Name from given strings.
func newNames(names ...string) []Name {
pn := make([]Name, len(names))
for i, n := range names {
pn[i] = name.New(n)
}
return pn
}
// freeNameser is an interface which Name should
// provide to have custom FreeNames implementation.
type freeNameser interface {
FreeNames() []Name
}
// FreeNames returns the free names in a give Name n.
func FreeNames(n Name) []Name {
if fn, ok := n.(freeNameser); ok {
return fn.FreeNames()
}
return []Name{n}
}
// freeVarser is an interface which Name should
// provide to have custom FreeVars implementation.
type freeVarser interface {
FreeVars() []Name
}
// FreeVars returns the free variables in a give Name n.
func FreeVars(n Name) []Name {
if fv, ok := n.(freeVarser); ok {
return fv.FreeVars()
}
return nil
}
// IsSameName is a simple comparison operator for Name.
// A Name x is equal with another Name y when x and y has the same name.
// This comparison ignores the underlying representation (sort, type, etc.).
func IsSameName(x, y Name) bool {
return x.Ident() == y.Ident()
}
func IsFreeName(x Name) bool {
return len(FreeNames(x)) == 1 && FreeNames(x)[0].Ident() == x.Ident()
}