Final thoughs on type parameter declarations
August 3, 2023
Are you as curious about Fuzzing in Go as I am? On Monday’s live stream, I’ll be live coding my way through John Arundel’s 4-part series on Go Fuzzing. I hope you’ll join me!
Two more small points before we move on from type parameter declarations…
Type parameter declarations
…
Type parameters may also be declared by the receiver specification of a method declaration associated with a generic type.
No surprises here. But we’ll look more closely when we get to the section on method declarations.
Within a type parameter list of a generic type
T
, a type constraint may not (directly, or indirectly through the type parameter list of another generic type) refer to `T``.type T1[P T1[P]] … // illegal: T1 refers to itself type T2[P interface{ T2[int] }] … // illegal: T2 refers to itself type T3[P interface{ m(T3[int])}] … // illegal: T3 refers to itself type T4[P T5[P]] … // illegal: T4 refers to T5 and type T5[P T4[P]] … // T5 refers to T4 type T6[P int] struct{ f *T6[P] } // ok: reference to T6 is not in type parameter list
No surprises here, either. By now, we’re pretty accustomed to the idea that Go doesn’t like circular references, be it in slice/array definitions, struct definitions, or even imports! So avoid circular references in your type parameter declarations, too!
Quotes from The Go Programming Language Specification Version of December 15, 2022