Source file src/internal/types/testdata/spec/methods.go

     1  // Copyright 2026 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  // Non-interface methods may declare type parameters.
     8  
     9  type T struct{}
    10  
    11  func (T) m[P any](x P) P { return x }
    12  
    13  func _() {
    14  	// A generic method must be instantiated before it is called.
    15  	var x T
    16  	var _ int = x.m[int](1) // explicit instantiation
    17  	var _ int = x.m(2)      // instantiation via type inference
    18  	var _ int = x /* ERROR "cannot use x.m(3.14) (value of type float64)" */ .m(3.14)
    19  
    20  	// Receivers of generic method calls may be complex expressions:
    21  	// Instantiation must work not just on simple operands.
    22  	var a [10]T
    23  	_ = a[0].m[int]  // explicit instantiation
    24  	_ = a[1].m(2.72) // instantiation via type inference
    25  
    26  	var m map[string][]struct{ T }
    27  	_ = m["foo"][0].T.m[float32]
    28  	_ = m["foo"][0].T.m(2.72)
    29  
    30  	_ = m["foo"][0].m[float32] // method promotion with explicit instantiation
    31  	_ = m["foo"][0].m(2.72)    // method promotion with instantiation via type inference
    32  
    33  	// A generic method expression may be assigned to a function after instantiation.
    34  	var _ func(T, int) int = T.m[int] // explicit instantiation
    35  	var _ func(T, int) int = T.m      // instantiation via type inference
    36  
    37  	// A generic method value may be assigned to a function after instantiation.
    38  	var _ func(int) int = x.m[int] // explicit instantiation
    39  	var _ func(int) int = x.m      // instantiation via type inference
    40  }
    41  
    42  // Generic methods may be added to generic types.
    43  type G[F any] struct {
    44  	f F
    45  }
    46  
    47  // The constraint for the method parameter may refer to the receiver type parameter.
    48  func (g G[F]) m[H interface{ convert(F) H }]() (r H) {
    49  	return r.convert(g.f)
    50  }
    51  
    52  // But the usual restrictions for type terms still apply.
    53  func (G[F]) m2[P F /* ERROR "cannot use a type parameter as constraint" */ ]() {}
    54  func (G[F]) m3[P *F]() {} // this is ok
    55  
    56  // Generic methods don't satisfy interfaces.
    57  type I[P any] interface {
    58  	m(P) P
    59  }
    60  
    61  var _ I[int] = T /* ERROR "(wrong type for method m)\n\t\thave m[P any](P) P\n\t\twant m(int) int" */ {}
    62  
    63  // A method declaring type parameters is generic even if it doesn't use the type parameters in its signature.
    64  type U struct {}
    65  
    66  func (U) m[_ any](x int) int { return x }
    67  
    68  var _ I[int] = U /* ERROR "wrong type for method m)\n\t\thave m[_ any](int) int\n\t\twant m(int) int" */ {}
    69  
    70  type J interface {
    71  	m()
    72  }
    73  
    74  type V struct {}
    75  
    76  func (V) m[_ any]() {}
    77  
    78  var _ J = V /* ERROR "wrong type for method m)\n\t\thave m[_ any]()\n\t\twant m()" */ {}
    79  
    80  // Test case from parser smoke test.
    81  
    82  type List[E any] []E
    83  
    84  func (l List[E]) Map[F any](m func(E) F) (r List[F]) {
    85  	for _, x := range l {
    86  		r = append(r, m(x))
    87  	}
    88  	return
    89  }
    90  
    91  func _() {
    92  	l := List[string]{"foo", "foobar", "42"}
    93  	r := l.Map(func(s string) int { return len(s)})
    94  	_ = r
    95  }
    96  
    97  func _[E, F any](l List[E]) List[F] {
    98  	var f func(List[E], func(E) F) List[F] = List[E].Map  // method expression & type inference
    99  	return f(l, func(E) F { var f F; return f })
   100  }
   101  
   102  func _[E, F any](l List[E]) List[F] {
   103  	var f func(func(E) F) List[F] = l.Map  // method value & type inference
   104  	return f(func(E) F { var f F; return f })
   105  }
   106  

View as plain text