...

Source file src/internal/types/testdata/examples/inference2.go

Documentation: internal/types/testdata/examples

     1  // Copyright 2023 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  // This file shows some examples of "reverse" type inference
     6  // where the type arguments for generic functions are determined
     7  // from assigning the functions.
     8  
     9  package p
    10  
    11  func f1[P any](P)        {}
    12  func f2[P any]() P       { var x P; return x }
    13  func f3[P, Q any](P) Q   { var x Q; return x }
    14  func f4[P any](P, P)     {}
    15  func f5[P any](P) []P    { return nil }
    16  func f6[P any](int) P    { var x P; return x }
    17  func f7[P any](P) string { return "" }
    18  
    19  // initialization expressions
    20  var (
    21  	v1           = f1 // ERROR "cannot use generic function f1 without instantiation"
    22  	v2 func(int) = f2 // ERROR "cannot infer P"
    23  
    24  	v3 func(int)     = f1
    25  	v4 func() int    = f2
    26  	v5 func(int) int = f3
    27  	_  func(int) int = f3[int]
    28  
    29  	v6 func(int, int)     = f4
    30  	v7 func(int, string)  = f4 // ERROR "inferred type func(int, int) for func(P, P) does not match type func(int, string) of v7"
    31  	v8 func(int) []int    = f5
    32  	v9 func(string) []int = f5 // ERROR "inferred type func(string) []string for func(P) []P does not match type func(string) []int of v9"
    33  
    34  	_, _ func(int) = f1, f1
    35  	_, _ func(int) = f1, f2 // ERROR "cannot infer P"
    36  )
    37  
    38  // Regular assignments
    39  func _() {
    40  	v1 = f1 // no error here because v1 is invalid (we don't know its type) due to the error above
    41  	var v1_ func() int
    42  	_ = v1_
    43  	v1_ = f1 // ERROR "cannot infer P"
    44  	v2 = f2  // ERROR "cannot infer P"
    45  
    46  	v3 = f1
    47  	v4 = f2
    48  	v5 = f3
    49  	v5 = f3[int]
    50  
    51  	v6 = f4
    52  	v7 = f4 // ERROR "inferred type func(int, int) for func(P, P) does not match type func(int, string) of v7"
    53  	v8 = f5
    54  	v9 = f5 // ERROR "inferred type func(string) []string for func(P) []P does not match type func(string) []int of v9"
    55  
    56  	// non-trivial LHS
    57  	var a [2]func(string) []int
    58  	a[0] = f5 // ERROR "inferred type func(string) []string for func(P) []P does not match type func(string) []int of a[0]"
    59  }
    60  
    61  // Return statements
    62  func _() func(int)     { return f1 }
    63  func _() func() int    { return f2 }
    64  func _() func(int) int { return f3 }
    65  func _() func(int) int { return f3[int] }
    66  
    67  func _() func(int, int) { return f4 }
    68  func _() func(int, string) {
    69  	return f4 /* ERROR "inferred type func(int, int) for func(P, P) does not match type func(int, string) of result variable" */
    70  }
    71  func _() func(int) []int { return f5 }
    72  func _() func(string) []int {
    73  	return f5 /* ERROR "inferred type func(string) []string for func(P) []P does not match type func(string) []int of result variable" */
    74  }
    75  
    76  func _() (_, _ func(int)) { return f1, f1 }
    77  func _() (_, _ func(int)) { return f1, f2 /* ERROR "cannot infer P" */ }
    78  
    79  // Argument passing
    80  func g1(func(int))                           {}
    81  func g2(func(int, int))                      {}
    82  func g3(func(int) string)                    {}
    83  func g4[P any](func(P) string)               {}
    84  func g5[P, Q any](func(P) string, func(P) Q) {}
    85  func g6(func(int), func(string))             {}
    86  
    87  func _() {
    88  	g1(f1)
    89  	g1(f2 /* ERROR "cannot infer P" */)
    90  	g2(f4)
    91  	g4(f6)
    92  	g5(f6, f7)
    93  	g6(f1, f1)
    94  }
    95  
    96  // Argument passing of partially instantiated functions
    97  func h(func(int, string), func(string, int)) {}
    98  
    99  func p[P, Q any](P, Q) {}
   100  
   101  func _() {
   102  	h(p, p)
   103  	h(p[int], p[string])
   104  }
   105  

View as plain text