Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit a3bc735

Browse files
author
Bryan C. Mills
committed
all: convert .go2 files to Go 1.21 .go files
1 parent 86572be commit a3bc735

24 files changed

+539
-443
lines changed

append.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build ignore
6+
57
// append illustrates the behavior of the "append" built-in in Go, for
68
// comparison to possible semantics using the Type Parameters draft design.
79
package main

append1.go2 append1.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build ignore
6+
57
// append1 illustrates the properties of the "append" variation described in the
68
// Type Parameters draft design.
79
package main
@@ -11,11 +13,11 @@ import (
1113
"fmt"
1214
)
1315

14-
func append(type T)(s []T, t ...T) []T {
16+
func append[T any](s []T, t ...T) []T {
1517
lens := len(s)
1618
tot := lens + len(t)
1719
if tot < 0 {
18-
panic("Append: cap out of range")
20+
panic("append: cap out of range")
1921
}
2022
if tot > cap(s) {
2123
news := make([]T, tot, tot+tot/2)
@@ -54,20 +56,23 @@ func main() {
5456
Ff := append(funcs, f)
5557
fmt.Printf("append(%T, %T) = %T\n", funcs, f, Ff)
5658

57-
fc := append(funcSlice, cancel)
59+
// cannot use funcSlice (variable of type []func()) as []context.CancelFunc value in argument to append
60+
fc := append[func()](funcSlice, cancel)
5861
fmt.Printf("append(%T, %T) = %T\n", funcSlice, cancel, fc)
5962

6063
cf := append(cancelSlice, f)
6164
fmt.Printf("append(%T, %T) = %T\n", cancelSlice, f, cf)
6265

6366
// returns type []func instead of Funcs
64-
Fc := append(funcs, cancel)
67+
// cannot use funcs (variable of type Funcs) as []context.CancelFunc value in argument to append
68+
Fc := append[func()](funcs, cancel)
6569
fmt.Printf("append(%T, %T) = %T\n", funcs, cancel, Fc)
6670

6771
Cc := append(cancels, f)
6872
fmt.Printf("append(%T, %T) = %T\n", cancels, f, Cc)
6973

70-
ffc := append(funcSlice, f, cancel)
74+
// cannot use funcSlice (variable of type []func()) as []context.CancelFunc value in argument to append
75+
ffc := append[func()](funcSlice, f, cancel)
7176
fmt.Printf("append(%T, %T, %T) = %T\n", funcSlice, f, cancel, ffc)
7277

7378
ff2 := append(funcSlice, funcSlice...)
@@ -81,12 +86,14 @@ func main() {
8186
Ff2 := append(funcs, funcSlice...)
8287
fmt.Printf("append(%T, %T...) = %T\n", funcs, funcSlice, Ff2)
8388

84-
// cannot use cancelSlice (variable of type []context.CancelFunc) as []func() value in argument
85-
// fc2 := append(funcSlice, cancelSlice...)
89+
// type []context.CancelFunc of cancelSlice does not match inferred type []func() for []T
90+
// cannot use cancelSlice (variable of type []context.CancelFunc) as []func() value in argument to append[func()]
91+
// fc2 := append[func()](funcSlice, cancelSlice...)
8692
// fmt.Printf("append(%T, %T...) = %T\n", funcSlice, cancelSlice, fc2)
8793

88-
// cannot use cancels (variable of type Cancels) as []func() value in argument
89-
// FC2 := append(funcs, cancels...)
94+
// type Cancels of cancels does not match inferred type []func() for []T
95+
// cannot use cancels (variable of type Cancels) as []func() value in argument to append[func()]
96+
// FC2 := append[func()](funcs, cancels...)
9097
// fmt.Printf("append(%T, %T...) = %T\n", funcs, cancels, FC2)
9198

9299
rr := append(recvSlice, r)
@@ -107,7 +114,8 @@ func main() {
107114
rr2 := append(recvSlice, recvSlice...)
108115
fmt.Printf("append(%T, %T...) = %T\n", recvSlice, recvSlice, rr2)
109116

110-
// cannot use bidiSlice (variable of type []chan int) as []<-chan int value in argument
111-
// rb2 := append(recvSlice, bidiSlice...)
117+
// type []chan int of bidiSlice does not match inferred type []<-chan int for []T
118+
// cannot use bidiSlice (variable of type []chan int) as []<-chan int value in argument to append[<-chan int]
119+
// rb2 := append[<-chan int](recvSlice, bidiSlice...)
112120
// fmt.Printf("append(%T, %T...) = %T\n", recvSlice, bidiSlice, rb2)
113121
}

append2.go2 append2.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build ignore
6+
57
// append2 illustrates the properties of a two-type-parameter "append" variant.
68
// With explicit type annotations, it seems to be able to handle the same cases
79
// as the existing built-in append.
@@ -15,13 +17,13 @@ import (
1517
"fmt"
1618
)
1719

18-
type sliceOf(type E) interface{ type []E }
20+
type sliceOf[E any] interface{ ~[]E }
1921

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 {
2123
lens := len(s)
2224
tot := lens + len(t)
2325
if tot < 0 {
24-
panic("Append: cap out of range")
26+
panic("append: cap out of range")
2527
}
2628
if tot > cap(s) {
2729
news := make([]T, tot, tot+tot/2)
@@ -59,23 +61,22 @@ func main() {
5961
Ff := append(funcs, f)
6062
fmt.Printf("append(%T, %T) = %T\n", funcs, f, Ff)
6163

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)
6466
fmt.Printf("append(%T, %T) = %T\n", funcSlice, cancel, fc)
6567

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)
6869
fmt.Printf("append(%T, %T) = %T\n", cancelSlice, f, cf)
6970

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)
7273
fmt.Printf("append(%T, %T) = %T\n", funcs, cancel, Fc)
7374

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)
7676
fmt.Printf("append(%T, %T) = %T\n", cancels, f, Cc)
7777

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)
7980
fmt.Printf("append(%T, %T, %T) = %T\n", funcSlice, f, cancel, ffc)
8081

8182
ff2 := append(funcSlice, funcSlice...)
@@ -87,27 +88,26 @@ func main() {
8788
Ff2 := append(funcs, funcSlice...)
8889
fmt.Printf("append(%T, %T...) = %T\n", funcs, funcSlice, Ff2)
8990

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...)
9294
// fmt.Printf("append(%T, %T...) = %T\n", funcSlice, cancelSlice, fc2)
9395

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...)
9699
// fmt.Printf("append(%T, %T...) = %T\n", funcs, cancels, FC2)
97100

98101
rr := append(recvSlice, r)
99102
fmt.Printf("append(%T, %T) = %T\n", recvSlice, r, rr)
100103

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)
103105
fmt.Printf("append(%T, %T) = %T\n", recvSlice, b, rb)
104106

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)
107108
fmt.Printf("append(%T, %T) = %T\n", RecvSlice, R, RR)
108109

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)
111111
fmt.Printf("append(%T, %T) = %T\n", RecvSlice, b, Rb)
112112

113113
rrb := append(recvSlice, r, b)
@@ -116,7 +116,7 @@ func main() {
116116
rr2 := append(recvSlice, recvSlice...)
117117
fmt.Printf("append(%T, %T...) = %T\n", recvSlice, recvSlice, rr2)
118118

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
120120
// rb2 := append(recvSlice, bidiSlice...)
121121
// fmt.Printf("append(%T, %T...) = %T\n", recvSlice, bidiSlice, rb2)
122122
}

append3.go

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build ignore
6+
7+
// append3 illustrates the properties of a 3-type-parameter "append" variant.
8+
//
9+
// With a more advanced type-inference algorithm and a proper "assignable to"
10+
// constraint, it could support inference for all of the same cases as the
11+
// built-in "append" does today, plus a few others.
12+
package main
13+
14+
import (
15+
"context"
16+
"fmt"
17+
"reflect"
18+
)
19+
20+
type sliceOf[E any] interface{ ~[]E }
21+
22+
// buggyAssignableTo simulates an “assignable to” type constraint using
23+
// something a little too permissive ("any").
24+
// We confirm assignability at run-time using the reflect package.
25+
type buggyAssignableTo[T any] interface { any }
26+
27+
func append[T any, T2 buggyAssignableTo[T], S sliceOf[T]](s S, t ...T2) S {
28+
// Confirm that T2 is assignable to T.
29+
// Ideally this should happen in the type system instead of at run-time.
30+
rt := reflect.TypeOf(s).Elem()
31+
rt2 := reflect.TypeOf(t).Elem()
32+
if !rt2.AssignableTo(rt) {
33+
panic("append: T2 is not assignable to T")
34+
}
35+
36+
lens := len(s)
37+
tot := lens + len(t)
38+
if tot < 0 {
39+
panic("append: cap out of range")
40+
}
41+
if tot > cap(s) {
42+
news := make([]T, tot, tot+tot/2)
43+
copy(news, s)
44+
s = news
45+
}
46+
47+
s = s[:tot]
48+
for i, x := range t {
49+
// We need to bounce through reflect because buggyAssignableTo doesn't
50+
// actually enable assignment.
51+
xt := reflect.ValueOf(x).Convert(rt).Interface().(T)
52+
s[lens+i] = xt
53+
}
54+
return s
55+
}
56+
57+
type Funcs []func()
58+
type Cancels []context.CancelFunc
59+
type Recv <-chan int
60+
61+
var (
62+
f func()
63+
cancel context.CancelFunc
64+
funcSlice []func()
65+
cancelSlice []context.CancelFunc
66+
funcs Funcs
67+
cancels Cancels
68+
r <-chan int
69+
recvSlice []<-chan int
70+
R Recv
71+
RecvSlice []Recv
72+
b chan int
73+
bidiSlice []chan int
74+
)
75+
76+
func main() {
77+
ff := append(funcSlice, f)
78+
fmt.Printf("append(%T, %T) = %T\n", funcSlice, f, ff)
79+
80+
Ff := append(funcs, f)
81+
fmt.Printf("append(%T, %T) = %T\n", funcs, f, Ff)
82+
83+
fc := append(funcSlice, cancel)
84+
fmt.Printf("append(%T, %T) = %T\n", funcSlice, cancel, fc)
85+
86+
cf := append(cancelSlice, f)
87+
fmt.Printf("append(%T, %T) = %T\n", cancelSlice, f, cf)
88+
89+
Fc := append(funcs, cancel)
90+
fmt.Printf("append(%T, %T) = %T\n", funcs, cancel, Fc)
91+
92+
Cc := append(cancels, f)
93+
fmt.Printf("append(%T, %T) = %T\n", cancels, f, Cc)
94+
95+
ffc := append(funcSlice, f, cancel)
96+
fmt.Printf("append(%T, %T, %T) = %T\n", funcSlice, f, cancel, ffc)
97+
98+
ff2 := append(funcSlice, funcSlice...)
99+
fmt.Printf("append(%T, %T...) = %T\n", funcSlice, funcSlice, ff2)
100+
101+
FF2 := append(funcs, funcs...)
102+
fmt.Printf("append(%T, %T...) = %T\n", funcs, funcs, FF2)
103+
104+
Ff2 := append(funcs, funcSlice...)
105+
fmt.Printf("append(%T, %T...) = %T\n", funcs, funcSlice, Ff2)
106+
107+
fc2 := append(funcSlice, cancelSlice...)
108+
fmt.Printf("append(%T, %T...) = %T\n", funcSlice, cancelSlice, fc2)
109+
110+
FC2 := append(funcs, cancels...)
111+
fmt.Printf("append(%T, %T...) = %T\n", funcs, cancels, FC2)
112+
113+
rr := append(recvSlice, r)
114+
fmt.Printf("append(%T, %T) = %T\n", recvSlice, r, rr)
115+
116+
rb := append(recvSlice, b)
117+
fmt.Printf("append(%T, %T) = %T\n", recvSlice, b, rb)
118+
119+
RR := append(RecvSlice, R)
120+
fmt.Printf("append(%T, %T) = %T\n", RecvSlice, R, RR)
121+
122+
Rb := append(RecvSlice, b)
123+
fmt.Printf("append(%T, %T) = %T\n", RecvSlice, b, Rb)
124+
125+
rrb := append(recvSlice, r, b)
126+
fmt.Printf("append(%T, %T) = %T\n", recvSlice, b, rrb)
127+
128+
rr2 := append(recvSlice, recvSlice...)
129+
fmt.Printf("append(%T, %T...) = %T\n", recvSlice, recvSlice, rr2)
130+
131+
rb2 := append(recvSlice, bidiSlice...)
132+
fmt.Printf("append(%T, %T...) = %T\n", recvSlice, bidiSlice, rb2)
133+
}

0 commit comments

Comments
 (0)