1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:04:47 -07:00

spec: explain in which situations function type arguments can be omitted

Change-Id: I9f008dba7ba6e30f0e62647482a3ed0b51bc1ad0
Reviewed-on: https://go-review.googlesource.com/c/go/+/502997
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Bypass: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2023-06-13 16:49:58 -07:00 committed by Robert Griesemer
parent ba4c6d1d6e
commit 01b649b7ef

View File

@ -1,6 +1,6 @@
<!--{ <!--{
"Title": "The Go Programming Language Specification", "Title": "The Go Programming Language Specification",
"Subtitle": "Version of June 13, 2023", "Subtitle": "Version of June 14, 2023",
"Path": "/ref/spec" "Path": "/ref/spec"
}--> }-->
@ -4340,24 +4340,46 @@ type parameter list type arguments after substitution
</pre> </pre>
<p> <p>
For a generic function, type arguments may be provided explicitly, or they When using a generic function, type arguments may be provided explicitly,
may be partially or completely <a href="#Type_inference">inferred</a>. or they may be partially or completely <a href="#Type_inference">inferred</a>
A generic function that is <i>not</i> <a href="#Calls">called</a> requires a from the context in which the function is used.
type argument list for instantiation; if the list is partial, all Provided that they can be inferred, type arguments may be omitted entirely if the function is:
remaining type arguments must be inferrable. </p>
A generic function that is called may provide a (possibly partial) type
argument list, or may omit it entirely if the omitted type arguments are <ul>
inferrable from the ordinary (non-type) function arguments. <li>
<a href="#Calls">called</a> with ordinary arguments,
</li>
<li>
<a href="#Assignment_statements">assigned</a> to a variable with an explicitly declared type,
</li>
<li>
<a href="#Calls">passed as an argument</a> to another function, or
</li>
<li>
<a href="#Return_statements">returned as a result</a>.
</li>
</ul>
<p>
In all other cases, a (possibly partial) type argument list must be present.
If a type argument list is absent or partial, all missing type arguments
must be inferrable from the context in which the function is used.
</p> </p>
<pre> <pre>
func min[T ~int|~float64](x, y T) T { … } // sum returns the sum (concatenation, for strings) of its arguments.
func sum[T ~int | ~float64 | ~string](x... T) T { … }
f := min // illegal: min must be instantiated with type arguments when used without being called x := sum // illegal: sum must have a type argument (x is a variable without a declared type)
minInt := min[int] // minInt has type func(x, y int) int intSum := sum[int] // intSum has type func(x... int) int
a := minInt(2, 3) // a has value 2 of type int a := intSum(2, 3) // a has value 5 of type int
b := min[float64](2.0, 3) // b has value 2.0 of type float64 b := sum[float64](2.0, 3) // b has value 5.0 of type float64
c := min(b, -1) // c has value -1.0 of type float64 c := sum(b, -1) // c has value 4.0 of type float64
type sumFunc func(x... string) string
var f sumFunc = sum // same as var f sumFunc = sum[string]
f = sum // same as f = sum[string]
</pre> </pre>
<p> <p>