3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test situations where functions/methods are not
8 // immediately called and we need to capture the dictionary
9 // required for later invocation.
20 genMethodExpressions[int](7)
22 genMethodValues[int](7)
30 func g1[T any](x T) T {
33 func g2[T any](x T) (T, T) {
49 panic("assertion failed")
55 panic("assertion failed")
59 type s[T any] struct {
65 func (x s[T]) g1() T {
68 func (x s[T]) g2() (T, T) {
72 func methodExpressions() {
88 func genMethodExpressions[T comparable](want T) {
95 if got := f1(x); got != want {
96 panic(fmt.Sprintf("f1(x) == %d, want %d", got, want))
99 if got := f1p(&x); got != want {
100 panic(fmt.Sprintf("f1p(&x) == %d, want %d", got, want))
103 if got1, got2 := f2(x); got1 != want || got2 != want {
104 panic(fmt.Sprintf("f2(x) == %d, %d, want %d, %d", got1, got2, want, want))
108 func methodValues() {
118 func genMethodValues[T comparable](want T) {
123 if got := f1(); got != want {
124 panic(fmt.Sprintf("f1() == %d, want %d", got, want))
127 if got1, got2 := f2(); got1 != want || got2 != want {
128 panic(fmt.Sprintf("f2() == %d, %d, want %d, %d", got1, got2, want, want))
137 var y interface{} = s[int]{a: 7}
139 func interfaceMethods() {
143 y.(interface{ g0() }).g0()
144 is7(y.(interface{ g1() int }).g1())
145 is77(y.(interface{ g2() (int, int) }).g2())
148 // Also check for instantiations outside functions.
157 var xtop = s[int]{a: 7}
176 if got, want := recur1[int](5), 110; got != want {
177 panic(fmt.Sprintf("recur1[int](5) = %d, want = %d", got, want))
181 type Integer interface {
185 func recur1[T Integer](n T) T {
186 if n == 0 || n == 1 {
189 return n * recur2(n-1)
193 func recur2[T Integer](n T) T {
195 for i, _ := range list {
199 for _, elt := range list {
202 return sum + recur1(n-1)