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.
9 // Parameterized type declarations
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"
14 type T2[P any] struct {
16 g int // int should still be in scope chain
21 // Alias type declarations cannot have type parameters.
22 // Issue #46477 proposes to change that.
23 type A1[P any] = /* ERROR "cannot be alias" */ struct{}
25 // Pending clarification of #46477 we disallow aliases
27 type A2 = List // ERROR "cannot use generic type"
34 // Parameterized type instantiations
37 type _ x /* ERROR "not a type" */ [int]
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"
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]
47 var _ T2[int] = T2[int]{}
49 var _ List[int] = []int{1, 2, 3}
50 var _ List[[]int] = [][]int{{1, 2, 3}}
51 var _ List[List[List[int]]]
53 // Parameterized types containing parameterized types
55 type T3[P any] List[P]
57 var _ T3[int] = T3[int](List[int]{1, 2, 3})
59 // Self-recursive generic types are not permitted
61 type self1[P any] self1 /* ERROR "invalid recursive type" */ [P]
62 type self2[P any] *self2[P] // this is ok