Class Central is learner-supported. When you buy through links on our site, we may earn an affiliate commission.

YouTube

Typing Generic Go

Gopher Academy via YouTube

Overview

Explore the latest generics design draft for Go in this GopherCon 2020 talk by Robert Griesemer. Dive into the concept of constrained type parameters, which enable generic functions and types in Go. Learn how type parameters integrate with Go's existing type system and gain insights into the compiler's process for type-checking generic Go code. Discover the intricacies of type parameter lists, constraints, instantiation, and invocation in generic calls. Examine the use of type lists in constraints and understand how to combine operations on different types. Gain a deeper understanding of the relationships between type parameters and explore practical examples of when to use generics in Go programming. Get updated on the Go team's progress towards implementing generics and the potential impact on coding styles in the language.

Syllabus

Intro
Type parameters draft design
A type parameter list
Type parameters to the rescue
Constraints • A constraint specifies the requirements which a type argument must satisfy
Declaration and scope of type parameters type parameter declaration
Type-checking the Sort call: Instantiation
Type-checking the Sort call: Invocation
Type-checking a generic call 1 Instantiation (new) • replace type parameters with type arguments in entire signature • verify that each type argument satisfies its constraint
Separating instantiation from invocation booksort :- Sort[book] // == #Sort book booksort (bookshelf)
Sort internals
Type-checking a generic call (refined) 1. If no type arguments are provided: Use argument type inference to infer type arguments.
What is missing? So far, constraints can only describe method requirements.
Type lists A constraint interface may have a list of types (besides methods)
Satisfying a type list An argument type satisfies a constraint with a type list if
Different type parameters are different types
Example: Combining byte and string operations type Bytes interface type []byte, string
Example: Relationships between type parameters type Pointer T any interface The type argument for PT must be a
Summary 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
How happy are we with this design? • Type parameters • Interfaces as constraints • Type lists in interfaces
With 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.
Examples (1)
When to use generics
Next steps The Go team is actively pursuing a real implementation (in a branch) so we can iron out any outstanding open problems.

Taught by

Gopher Academy

Reviews

Start your review of Typing Generic Go

Never Stop Learning.

Get personalized course recommendations, track subjects and courses with reminders, and more.

Someone learning on their laptop while sitting on the floor.