Circular generic types
July 27, 2023
There’s a lot to cover within type definitions! We’re several days in, and still going strong…
Type definitions
…
In a type definition the given type cannot be a type parameter.
type T[P any] P // illegal: P is a type parameter func f[T any]() { type L T // illegal: T is a type parameter declared by the enclosing function }
I expect this will surprise noone. We’ve already seen that struct, array, and slice types cannot reference themselves. This is just the logical application of the same idea to generic types.
Quotes from The Go Programming Language Specification Version of December 15, 2022