File: generics.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20231006.7918f67-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 6,492 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 27
file content (84 lines) | stat: -rw-r--r-- 1,065 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package p

//// Generics

// old
type G[T any] []T

// new
// OK: param name change
type G[A any] []A

// old
type GM[A, B comparable] map[A]B

// new
// i GM: changed from map[A]B to map[B]A
type GM[A, B comparable] map[B]A

// old
type GT[V any] struct {
}

func (GT[V]) M(*GT[V]) {}

// new
// OK
type GT[V any] struct {
}

func (GT[V]) M(*GT[V]) {}

// old
type GT2[V any] struct {
}

func (GT2[V]) M(*GT2[V]) {}

// new
// i GT2: changed from GT2[V any] to GT2[V comparable]
type GT2[V comparable] struct {
}

func (GT2[V]) M(*GT2[V]) {}

// both
type custom interface {
	int
}

type GT3[E custom] map[E]int

// Instantiated types:
// Two instantiations of generic types
// with different type parameters are different.

// both
type H[T any] []T

// old
var V1 H[int]

type T int

var V2 H[T]

// new
// i V1: changed from H[int] to H[bool]
var V1 H[bool]

// i T: changed from int to bool
type T bool

// OK: we reported on T, so we don't need to here.
var V2 H[T]

// old
type t int

// new
// i t: changed from int to byte
type t byte

// both
var V3 H[t]