@@ -14,46 +14,46 @@ import (
14
14
15
15
// A Union represents a union of terms embedded in an interface.
16
16
type Union struct {
17
- terms []* term // list of syntactical terms (not a canonicalized termlist)
17
+ terms []* Term // list of syntactical terms (not a canonicalized termlist)
18
18
tset * _TypeSet // type set described by this union, computed lazily
19
19
}
20
20
21
- // NewUnion returns a new Union type with the given terms (types[i], tilde[i]).
22
- // The lengths of both arguments must match. It is an error to create an empty
23
- // union; they are syntactically not possible.
24
- func NewUnion (types []Type , tilde []bool ) * Union { return newUnion (types , tilde ) }
21
+ // NewUnion returns a new Union type with the given terms.
22
+ // It is an error to create an empty union; they are syntactically not possible.
23
+ func NewUnion (terms []* Term ) * Union {
24
+ if len (terms ) == 0 {
25
+ panic ("empty union" )
26
+ }
27
+ return & Union {terms , nil }
28
+ }
25
29
26
- func (u * Union ) IsEmpty () bool { return len (u .terms ) == 0 }
27
- func (u * Union ) NumTerms () int { return len (u .terms ) }
28
- func (u * Union ) Term (i int ) (Type , bool ) { t := u .terms [i ]; return t .typ , t .tilde }
30
+ func (u * Union ) Len () int { return len (u .terms ) }
31
+ func (u * Union ) Term (i int ) * Term { return u .terms [i ] }
29
32
30
33
func (u * Union ) Underlying () Type { return u }
31
34
func (u * Union ) String () string { return TypeString (u , nil ) }
32
35
36
+ // A Term represents a term in a Union.
37
+ type Term term
38
+
39
+ // NewTerm returns a new union term.
40
+ func NewTerm (tilde bool , typ Type ) * Term { return & Term {tilde , typ } }
41
+
42
+ func (t * Term ) Tilde () bool { return t .tilde }
43
+ func (t * Term ) Type () Type { return t .typ }
44
+ func (t * Term ) String () string { return (* term )(t ).String () }
45
+
33
46
// ----------------------------------------------------------------------------
34
47
// Implementation
35
48
36
- func newUnion (types []Type , tilde []bool ) * Union {
37
- assert (len (types ) == len (tilde ))
38
- if len (types ) == 0 {
39
- panic ("empty union" )
40
- }
41
- t := new (Union )
42
- t .terms = make ([]* term , len (types ))
43
- for i , typ := range types {
44
- t .terms [i ] = & term {tilde [i ], typ }
45
- }
46
- return t
47
- }
48
-
49
49
func parseUnion (check * Checker , tlist []ast.Expr ) Type {
50
- var terms []* term
50
+ var terms []* Term
51
51
for _ , x := range tlist {
52
52
tilde , typ := parseTilde (check , x )
53
53
if len (tlist ) == 1 && ! tilde {
54
54
return typ // single type
55
55
}
56
- terms = append (terms , & term { tilde , typ } )
56
+ terms = append (terms , NewTerm ( tilde , typ ) )
57
57
}
58
58
59
59
// Check validity of terms.
@@ -128,15 +128,15 @@ func parseTilde(check *Checker, x ast.Expr) (tilde bool, typ Type) {
128
128
// overlappingTerm reports the index of the term x in terms which is
129
129
// overlapping (not disjoint) from y. The result is < 0 if there is no
130
130
// such term.
131
- func overlappingTerm (terms []* term , y * term ) int {
131
+ func overlappingTerm (terms []* Term , y * Term ) int {
132
132
for i , x := range terms {
133
133
// disjoint requires non-nil, non-top arguments
134
134
if debug {
135
135
if x == nil || x .typ == nil || y == nil || y .typ == nil {
136
136
panic ("empty or top union term" )
137
137
}
138
138
}
139
- if ! x .disjoint (y ) {
139
+ if ! ( * term )( x ) .disjoint (( * term )( y ) ) {
140
140
return i
141
141
}
142
142
}
0 commit comments