Type inference
…
Given a set of type equations, the type parameters to solve for are the type parameters of the functions that need to be instantiated and for which no explicit type arguments is provided. These type parameters are called bound type parameters. For instance, in the dedup example above, the type parameters
P
andE
are bound todedup
. An argument to a generic function call may be a generic function itself. The type parameters of that function are included in the set of bound type parameters. The types of function arguments may contain type parameters from other functions (such as a generic function enclosing a function call). Those type parameters may also appear in type equations but they are not bound in that context. Type equations are always solved for the bound type parameters only.
Now I’m pretty sure there’s a typo in the above. I’m quite sure it means “type parameters S
and E
” not “P
and E
” (I’ll try to remember to submit a CL later to fix it…). So with that in mind, let’s continue.
Now let’s try to make sense of this, now, with some examples.
First, it references the dedup
example we quoted yesterday. Here it is again:
// dedup returns a copy of the argument slice with any duplicate entries removed. func dedup[S ~[]E, E comparable](S) S { … } type Slice []int var s Slice s = dedup(s) // same as s = dedup[Slice, int](s)
As it (almost) says, the type parameters S
and E
are said to be bound to dedup
. Why does this matter?
Because there can be additional type parameters at play, if we use generic functions as arguments to generic functions. Let’s expand the example:
// sort orders the elements of a slice
func sort[X ~[]Y, Y comparable](X) X { … }
sort(dedup([]int{1, 2, 3, 2, 1}))
Now we have the generic dedup
functioon being called, which has bound type parameters S
and E
as previously discussed. But its result is passed to sort
, which itself has bound parameters X
and Y
. What is noteworthy is that S
and E
are not bound to sort
.
Tomorrow we’ll plug this new concept into the nitty-gritty of the logic of type equation solutions. I hope you’re ready!
Quotes from The Go Programming Language Specification Version of August 2, 2023