~/talk

Typing [Generic] Go

Talkat GopherCon 2020 · 2020-11-09..11-13· [compilers-and-language-design, go]
slides:none on file

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

  1. 0:00Intro
  2. 1:33Type parameters draft design
  3. 3:06A type parameter list
  4. 4:44Type parameters to the rescue
  5. 5:26Constraints • A constraint specifies the requirements which a type argument must satisfy
  6. 6:54Declaration and scope of type parameters type parameter declaration
  7. 8:01Type-checking the Sort call: Instantiation
  8. 9:21Type-checking the Sort call: Invocation
  9. 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. 10:22Separating instantiation from invocation booksort :- Sort[book] // == #Sort book booksort (bookshelf)
  11. 13:12Sort internals
  12. 17:31Type-checking a generic call (refined) 1. If no type arguments are provided: Use argument type inference to infer type arguments.
  13. 18:32What is missing? So far, constraints can only describe method requirements.
  14. 19:23Type lists A constraint interface may have a list of types (besides methods)
  15. 20:12Satisfying a type list An argument type satisfies a constraint with a type list if
  16. 21:58Different type parameters are different types
  17. 23:20Example: Combining byte and string operations type Bytes interface type []byte, string
  18. 23:59Example: Relationships between type parameters type Pointer T any interface The type argument for PT must be a
  19. 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
  20. 27:02How happy are we with this design? • Type parameters • Interfaces as constraints • Type lists in interfaces
  21. 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.
  22. 30:11Examples (1)
  23. 31:47When to use generics
  24. 32:53Next steps The Go team is actively pursuing a real implementation (in a branch) so we can iron out any outstanding open problems.
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.