Methods on generic types

September 6, 2023

Ready to dive back into generics, this time with regard to methods? Great, ‘cause here we go!

Method declarations

If the receiver base type is a generic type, the receiver specification must declare corresponding type parameters for the method to use.

Okay. Before we continue, let’s make sure we all know exactly what we’re talking about. What does it mean for a receiver base type to be a generic type? I think this is best illustrated by the example which comes a bit later in the spec:

type Pair[A, B any] struct {
	a A
	b B
}

Okay. So given the above type Pair[A, B any], if we define a method on it, the receiver’s base type would be a generic type:

func (p Pair[A, B]) Swap() Pair[B, A]  { … }

Hopefully now it makes a bit more sense exactly what we’re talking about. So let’s continue.

This (declaring the corresponding type parameters) makes the receiver type parameters available to the method.

In other words, this would be invalid:

type Pair[A, B any] struct {
  a A
  b B
}

func (p Pair) Swap() { ... }

because there’s then no way for the method to know the type parameters’ types.

Quotes from The Go Programming Language Specification Version of August 2, 2023


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Type parameters in method definitions

Yesterday we saw that when a method is defined on a generic type, the receiver must include type parameters. Now for all the relevant details: Method declarations … … Syntactically, this type parameter declaration looks like an instantiation of the receiver base type: the type arguments must be identifiers denoting the type parameters being declared, one for each type parameter of the receiver base type. The type parameter names do not need to match their corresponding parameter names in the receiver base type definition, and all non-blank parameter names must be unique in the receiver parameter section and the method signature.


Go 1.24.0 is here!

Go 1.24 is released! Yay! That means a short re-visit to the Go spec, as we look at things that have changed. From the release notes we see that there’s really only one significant change: Changes to the language¶ Go 1.24 now fully supports generic type aliases: a type alias may be parameterized like a defined type. See the language spec for details. For now, the feature can be disabled by setting GOEXPERIMENT=noaliastypeparams; but the aliastypeparams setting will be removed for Go 1.


Method values

A quick reminder: In today’s live stream, I’ll be deploying a Go app to Kubernetes! I hope you’ll join me! Today we continue the explanation of Method values. There’s nothing too surprising today. The spec mostly just confirms what should seem natural… Method values … As in the discussion of method expressions above, consider a struct type T with two methods, Mv, whose receiver is of type T, and Mp, whose receiver is of type *T.

Get daily content like this in your inbox!

Subscribe