Skip to content

Commit 01b649b

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Bypass: Robert Griesemer <[email protected]>
1 parent ba4c6d1 commit 01b649b

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

doc/go_spec.html

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of June 13, 2023",
3+
"Subtitle": "Version of June 14, 2023",
44
"Path": "/ref/spec"
55
}-->
66

@@ -4340,24 +4340,46 @@ <h3 id="Instantiations">Instantiations</h3>
43404340
</pre>
43414341

43424342
<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.
43514368
</p>
43524369

43534370
<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
43554379

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]
43614383
</pre>
43624384

43634385
<p>

0 commit comments

Comments
 (0)