Skip to content

Commit 4f51db2

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
doc/go1.21: document type inference changes
For #39661. For #41176. For #51593. For #52397. For #57192. For #58645. For #58650. For #58671. For #59338. For #59750. For #60353. Change-Id: Ib731c9f2879beb541f44cb10e40c36a8677d3ad4 Reviewed-on: https://go-review.googlesource.com/c/go/+/499282 TryBot-Bypass: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 01e703c commit 4f51db2

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

doc/go1.21.html

+51-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,57 @@ <h2 id="language">Changes to the language</h2>
7070
spec in past releases. The new rule provides an unambiguous definition.
7171
</p>
7272

73-
<p><!-- https://go.dev/issue/59338 -->
74-
TODO: <a href="https://go.dev/issue/59338">https://go.dev/issue/59338</a>: infer type arguments from assignments of generic functions (reverse type inference)
73+
<p>
74+
Multiple improvements that increase the power and precision of type inference have been made.
75+
</p>
76+
<ul>
77+
<li><!-- https://go.dev/issue/59338 -->
78+
A (possibly partially instantiated generic) function may now be called with arguments that are
79+
themselves (possibly partially instantiated) generic functions.
80+
The compiler will attempt to infer the missing type arguments of the callee (as before) and,
81+
for each argument that is a generic function that is not fully instantiated,
82+
its missing type arguments (new).
83+
Typical use cases are calls to generic functions operating on containers
84+
(such as <a href="/pkg/slices#IndexFunc">slices.IndexFunc</a>) where a function argument
85+
may also be generic, and where the type argument of the called function and its arguments
86+
are inferred from the container type.
87+
More generally, a generic function may now be used without explicit instantiation when
88+
it is assigned to a variable or returned as a result value if the type arguments can
89+
be inferred from the assignment.
90+
</li>
91+
<li><!-- https://go.dev/issue/60353, https://go.dev/issue/57192, https://go.dev/issue/52397, https://go.dev/issue/41176 -->
92+
Type inference now also considers methods when a value is assigned to an interface:
93+
type arguments for type parameters used in method signatures may be inferred from
94+
the corresponding parameter types of matching methods.
95+
</li>
96+
<li><!-- https://go.dev/issue/51593 https://go.dev/issue/39661 -->
97+
Similarly, since a type argument must implement all the methods of its corresponding constraint,
98+
the methods of the type argument and constraint are matched which may lead to the inference of
99+
additional type arguments.
100+
</li>
101+
<li><!-- https://go.dev/issue/58671 -->
102+
If multiple untyped constant arguments of different kinds (such as an untyped int and
103+
an untyped floating-point constant) are passed to parameters with the same (not otherwise
104+
specified) type parameter type, instead of an error, now type inference determines the
105+
type using the same approach as an operator with untyped constant operands.
106+
This change brings the types inferred from untyped constant arguments in line with the
107+
types of constant expressions.
108+
</li>
109+
<li><!-- https://go.dev/issue/59750 -->
110+
Type inference is now precise when matching corresponding types in assignments:
111+
component types (such as the the elements of slices, or the parameter types in function signatures)
112+
must be identical (given suitable type arguments) to match, otherwise inference fails.
113+
This change produces more accurate error messages:
114+
where in the past type inference may have succeeded incorrectly and lead to an invalid assignment,
115+
the compiler now reports an inference error if two types can't possibly match.
116+
</li>
117+
</ul>
118+
119+
<p><!-- https://go.dev/issue/58650 -->
120+
More generally, the description of
121+
<a href="https://tip.golang.org/ref/spec#Type_inference">type inference</a>
122+
in the language spec has been clarified.
123+
Together, all these changes make type inference more powerful and inference failures less surprising.
75124
</p>
76125

77126
<p><!-- https://go.dev/issue/56986 -->

0 commit comments

Comments
 (0)