|
1 | 1 | <!--{
|
2 | 2 | "Title": "The Go Programming Language Specification",
|
3 |
| - "Subtitle": "Version of June 13, 2023", |
| 3 | + "Subtitle": "Version of June 14, 2023", |
4 | 4 | "Path": "/ref/spec"
|
5 | 5 | }-->
|
6 | 6 |
|
@@ -4340,24 +4340,46 @@ <h3 id="Instantiations">Instantiations</h3>
|
4340 | 4340 | </pre>
|
4341 | 4341 |
|
4342 | 4342 | <p>
|
4343 |
| -For a generic function, type arguments may be provided explicitly, or they |
4344 |
| -may be partially or completely <a href="#Type_inference">inferred</a>. |
4345 |
| -A generic function that is <i>not</i> <a href="#Calls">called</a> requires a |
4346 |
| -type argument list for instantiation; if the list is partial, all |
4347 |
| -remaining type arguments must be inferrable. |
4348 |
| -A generic function that is called may provide a (possibly partial) type |
4349 |
| -argument list, or may omit it entirely if the omitted type arguments are |
4350 |
| -inferrable from the ordinary (non-type) function arguments. |
| 4343 | +When using a generic function, type arguments may be provided explicitly, |
| 4344 | +or they may be partially or completely <a href="#Type_inference">inferred</a> |
| 4345 | +from the context in which the function is used. |
| 4346 | +Provided that they can be inferred, type arguments may be omitted entirely if the function is: |
| 4347 | +</p> |
| 4348 | + |
| 4349 | +<ul> |
| 4350 | +<li> |
| 4351 | + <a href="#Calls">called</a> with ordinary arguments, |
| 4352 | +</li> |
| 4353 | +<li> |
| 4354 | + <a href="#Assignment_statements">assigned</a> to a variable with an explicitly declared type, |
| 4355 | +</li> |
| 4356 | +<li> |
| 4357 | + <a href="#Calls">passed as an argument</a> to another function, or |
| 4358 | +</li> |
| 4359 | +<li> |
| 4360 | + <a href="#Return_statements">returned as a result</a>. |
| 4361 | +</li> |
| 4362 | +</ul> |
| 4363 | + |
| 4364 | +<p> |
| 4365 | +In all other cases, a (possibly partial) type argument list must be present. |
| 4366 | +If a type argument list is absent or partial, all missing type arguments |
| 4367 | +must be inferrable from the context in which the function is used. |
4351 | 4368 | </p>
|
4352 | 4369 |
|
4353 | 4370 | <pre>
|
4354 |
| -func min[T ~int|~float64](x, y T) T { … } |
| 4371 | +// sum returns the sum (concatenation, for strings) of its arguments. |
| 4372 | +func sum[T ~int | ~float64 | ~string](x... T) T { … } |
| 4373 | + |
| 4374 | +x := sum // illegal: sum must have a type argument (x is a variable without a declared type) |
| 4375 | +intSum := sum[int] // intSum has type func(x... int) int |
| 4376 | +a := intSum(2, 3) // a has value 5 of type int |
| 4377 | +b := sum[float64](2.0, 3) // b has value 5.0 of type float64 |
| 4378 | +c := sum(b, -1) // c has value 4.0 of type float64 |
4355 | 4379 |
|
4356 |
| -f := min // illegal: min must be instantiated with type arguments when used without being called |
4357 |
| -minInt := min[int] // minInt has type func(x, y int) int |
4358 |
| -a := minInt(2, 3) // a has value 2 of type int |
4359 |
| -b := min[float64](2.0, 3) // b has value 2.0 of type float64 |
4360 |
| -c := min(b, -1) // c has value -1.0 of type float64 |
| 4380 | +type sumFunc func(x... string) string |
| 4381 | +var f sumFunc = sum // same as var f sumFunc = sum[string] |
| 4382 | +f = sum // same as f = sum[string] |
4361 | 4383 | </pre>
|
4362 | 4384 |
|
4363 | 4385 | <p>
|
|
0 commit comments