From: Robert Griesemer Date: Tue, 13 Jun 2023 23:49:58 +0000 (-0700) Subject: spec: explain in which situations function type arguments can be omitted X-Git-Tag: go1.21rc1~4 X-Git-Url: http://www.git.cypherpunks.ru/?a=commitdiff_plain;h=01b649b7ef45b89610a47efa048b8e73e76b078e;p=gostls13.git 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 Reviewed-by: Robert Griesemer Reviewed-by: Ian Lance Taylor TryBot-Bypass: Robert Griesemer --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 6e735e4373..c2fa871eaa 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -4340,24 +4340,46 @@ type parameter list type arguments after substitution

-For a generic function, type arguments may be provided explicitly, or they -may be partially or completely inferred. -A generic function that is not called requires a -type argument list for instantiation; if the list is partial, all -remaining type arguments must be inferrable. -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 -inferrable from the ordinary (non-type) function arguments. +When using a generic function, type arguments may be provided explicitly, +or they may be partially or completely inferred +from the context in which the function is used. +Provided that they can be inferred, type arguments may be omitted entirely if the function is: +

+ + + +

+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.

-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 { … }
+
+x := sum                       // illegal: sum must have a type argument (x is a variable without a declared type)
+intSum := sum[int]             // intSum has type func(x... int) int
+a := intSum(2, 3)              // a has value 5 of type int
+b := sum[float64](2.0, 3)      // b has value 5.0 of type float64
+c := sum(b, -1)                // c has value 4.0 of type float64
 
-f := min                   // illegal: min must be instantiated with type arguments when used without being called
-minInt := min[int]         // minInt has type func(x, y int) int
-a := minInt(2, 3)          // a has value 2 of type int
-b := min[float64](2.0, 3)  // b has value 2.0 of type float64
-c := min(b, -1)            // c has value -1.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]