1 // Copyright 2019 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 package p
6
7 type myInt int
8
9 // Parameterized type declarations
10
11 // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
12 type T1[P any] P // ERROR cannot use a type parameter as RHS in type declaration
13
14 type T2[P any] struct {
15 f P
16 g int // int should still be in scope chain
17 }
18
19 type List[P any] []P
20
21 // Alias type declarations cannot have type parameters.
22 // Issue #46477 proposses to change that.
23 type A1[P any] = /* ERROR cannot be alias */ struct{}
24
25 // Pending clarification of #46477 we disallow aliases
26 // of generic types.
27 type A2 = List // ERROR cannot use generic type
28 var _ A2[int]
29 var _ A2
30
31 type A3 = List[int]
32 var _ A3
33
34 // Parameterized type instantiations
35
36 var x int
37 type _ x /* ERROR not a type */ [int]
38
39 type _ int /* ERROR not a generic type */ [] // ERROR expected type argument list
40 type _ myInt /* ERROR not a generic type */ [] // ERROR expected type argument list
41
42 // TODO(gri) better error messages
43 type _ T1[] // ERROR expected type argument list
44 type _ T1[x /* ERROR not a type */ ]
45 type _ T1 /* ERROR got 2 arguments but 1 type parameters */ [int, float32]
46
47 var _ T2[int] = T2[int]{}
48
49 var _ List[int] = []int{1, 2, 3}
50 var _ List[[]int] = [][]int{{1, 2, 3}}
51 var _ List[List[List[int]]]
52
53 // Parameterized types containing parameterized types
54
55 type T3[P any] List[P]
56
57 var _ T3[int] = T3[int](List[int]{1, 2, 3})
58
59 // Self-recursive generic types are not permitted
60
61 type self1[P any] self1 /* ERROR illegal cycle */ [P]
62 type self2[P any] *self2[P] // this is ok
63
View as plain text