Typing [Generic] Go
summary
Latest draft adds interfaces as constraints and type lists so generics use constrained type parameters, remain 100% backward compatible with no new keywords, and rely on compile-time instantiation plus optional argument type inference. Examples include a generic sort that instantiates to a concrete function, numeric ops via an ordered typelist, and pointer-pair constraints that enable constraint type inference. A prototype translator to regular Go exists but a native implementation is still in progress.
AI-written summary · about
chapters (24)
- 0:00Intro
- 1:33Type parameters draft design
- 3:06A type parameter list
- 4:44Type parameters to the rescue
- 5:26Constraints • A constraint specifies the requirements which a type argument must satisfy
- 6:54Declaration and scope of type parameters type parameter declaration
- 8:01Type-checking the Sort call: Instantiation
- 9:21Type-checking the Sort call: Invocation
- 9:44Type-checking a generic call 1 Instantiation (new) • replace type parameters with type arguments in entire signature • verify that each type argument satisfies its constraint
- 10:22Separating instantiation from invocation booksort :- Sort[book] // == #Sort book booksort (bookshelf)
- 13:12Sort internals
- 17:31Type-checking a generic call (refined) 1. If no type arguments are provided: Use argument type inference to infer type arguments.
- 18:32What is missing? So far, constraints can only describe method requirements.
- 19:23Type lists A constraint interface may have a list of types (besides methods)
- 20:12Satisfying a type list An argument type satisfies a constraint with a type list if
- 21:58Different type parameters are different types
- 23:20Example: Combining byte and string operations type Bytes interface type []byte, string
- 23:59Example: Relationships between type parameters type Pointer T any interface The type argument for PT must be a
- 26:07Summary Declarations Type parameter lists are like ordinary parameter lists with TT • Function and type declarations may have type parameter lists • Type parameters are constrained by interfaces
- 27:02How happy are we with this design? • Type parameters • Interfaces as constraints • Type lists in interfaces
- 29:18With great power comes great responsibility • Type parameters ('generics ) are a new tool in the toolset of Go. • Orthogonal to the rest of the language. • Orthogonality opens a new dimension of coding styles.
- 30:11Examples (1)
- 31:47When to use generics
- 32:53Next steps The Go team is actively pursuing a real implementation (in a branch) so we can iron out any outstanding open problems.








