2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
5
+ //go:build ignore
6
+
5
7
// append2 illustrates the properties of a two-type-parameter "append" variant.
6
8
// With explicit type annotations, it seems to be able to handle the same cases
7
9
// as the existing built-in append.
@@ -15,13 +17,13 @@ import (
15
17
"fmt"
16
18
)
17
19
18
- type sliceOf(type E) interface{ type []E }
20
+ type sliceOf [ E any ] interface { ~ []E }
19
21
20
- func append(type S sliceOf(T), T interface{}) (s S, t ...T) S {
22
+ func append [ T any , S sliceOf [ T ]] (s S , t ... T ) S {
21
23
lens := len (s )
22
24
tot := lens + len (t )
23
25
if tot < 0 {
24
- panic("Append : cap out of range")
26
+ panic ("append : cap out of range" )
25
27
}
26
28
if tot > cap (s ) {
27
29
news := make ([]T , tot , tot + tot / 2 )
@@ -59,23 +61,22 @@ func main() {
59
61
Ff := append (funcs , f )
60
62
fmt .Printf ("append(%T, %T) = %T\n " , funcs , f , Ff )
61
63
62
- // []func() does not satisfy sliceOf(T) ([]func() not found in []context.CancelFunc)
63
- fc := append([] func(), func()) (funcSlice, cancel)
64
+ // []func() does not satisfy sliceOf[context.CancelFunc] ([]func() missing in ~ []context.CancelFunc)
65
+ fc := append [ func ()] (funcSlice , cancel )
64
66
fmt .Printf ("append(%T, %T) = %T\n " , funcSlice , cancel , fc )
65
67
66
- // []context.CancelFunc does not satisfy sliceOf(T) ([]context.CancelFunc not found in []func())
67
- cf := append([]context.CancelFunc, context.CancelFunc)(cancelSlice, f)
68
+ cf := append (cancelSlice , f )
68
69
fmt .Printf ("append(%T, %T) = %T\n " , cancelSlice , f , cf )
69
70
70
- // Funcs does not satisfy sliceOf(T) ([]func() not found in []context.CancelFunc)
71
- Fc := append(Funcs, func()) (funcs, cancel)
71
+ // Funcs does not satisfy sliceOf[context.CancelFunc] (Funcs missing in ~ []context.CancelFunc)
72
+ Fc := append [ func ()] (funcs , cancel )
72
73
fmt .Printf ("append(%T, %T) = %T\n " , funcs , cancel , Fc )
73
74
74
- // Cancels does not satisfy sliceOf(T) ([]context.CancelFunc not found in []func())
75
- Cc := append(Cancels, context.CancelFunc)(cancels, f)
75
+ Cc := append (cancels , f )
76
76
fmt .Printf ("append(%T, %T) = %T\n " , cancels , f , Cc )
77
77
78
- ffc := append(funcSlice, f, cancel)
78
+ // []func() does not satisfy sliceOf[context.CancelFunc] ([]func() missing in ~[]context.CancelFunc)
79
+ ffc := append [func ()](funcSlice , f , cancel )
79
80
fmt .Printf ("append(%T, %T, %T) = %T\n " , funcSlice , f , cancel , ffc )
80
81
81
82
ff2 := append (funcSlice , funcSlice ... )
@@ -87,27 +88,26 @@ func main() {
87
88
Ff2 := append (funcs , funcSlice ... )
88
89
fmt .Printf ("append(%T, %T...) = %T\n " , funcs , funcSlice , Ff2 )
89
90
90
- // []func() does not satisfy sliceOf(T) ([]func() not found in []context.CancelFunc)
91
- // fc2 := append(funcSlice, cancelSlice...)
91
+ // []func() does not satisfy sliceOf[context.CancelFunc] ([]func() missing in ~[]context.CancelFunc)
92
+ // cannot use cancelSlice (variable of type []context.CancelFunc) as []func() value in argument to append[func()]
93
+ // fc2 := append[func()](funcSlice, cancelSlice...)
92
94
// fmt.Printf("append(%T, %T...) = %T\n", funcSlice, cancelSlice, fc2)
93
95
94
- // Funcs does not satisfy sliceOf(T) ([]func() not found in []context.CancelFunc)
95
- // FC2 := append(funcs, cancels...)
96
+ // Funcs does not satisfy sliceOf[context.CancelFunc] (Funcs missing in ~[]context.CancelFunc)
97
+ // cannot use cancels (variable of type Cancels) as []func() value in argument to append[func()]
98
+ // FC2 := append[func()](funcs, cancels...)
96
99
// fmt.Printf("append(%T, %T...) = %T\n", funcs, cancels, FC2)
97
100
98
101
rr := append (recvSlice , r )
99
102
fmt .Printf ("append(%T, %T) = %T\n " , recvSlice , r , rr )
100
103
101
- // []<-chan int does not satisfy sliceOf(T) ([]<-chan int not found in []chan int)
102
- rb := append([]<-chan int, <-chan int)(recvSlice, b)
104
+ rb := append (recvSlice , b )
103
105
fmt .Printf ("append(%T, %T) = %T\n " , recvSlice , b , rb )
104
106
105
- // main.Recv undefined (type func() has no field or method Recv) (http://b/159049072)
106
- RR := append([]Recv, Recv)(RecvSlice, R)
107
+ RR := append (RecvSlice , R )
107
108
fmt .Printf ("append(%T, %T) = %T\n " , RecvSlice , R , RR )
108
109
109
- // []Recv does not satisfy sliceOf(T) ([]Recv not found in []chan int)
110
- Rb := append([]Recv, Recv)(RecvSlice, b)
110
+ Rb := append (RecvSlice , b )
111
111
fmt .Printf ("append(%T, %T) = %T\n " , RecvSlice , b , Rb )
112
112
113
113
rrb := append (recvSlice , r , b )
@@ -116,7 +116,7 @@ func main() {
116
116
rr2 := append (recvSlice , recvSlice ... )
117
117
fmt .Printf ("append(%T, %T...) = %T\n " , recvSlice , recvSlice , rr2 )
118
118
119
- // []<-chan int does not satisfy sliceOf(T) ( []<-chan int not found in []chan int)
119
+ // cannot use bidiSlice (variable of type []chan int) as []<-chan int value in argument to append
120
120
// rb2 := append(recvSlice, bidiSlice...)
121
121
// fmt.Printf("append(%T, %T...) = %T\n", recvSlice, bidiSlice, rb2)
122
122
}
0 commit comments