Typing Generic Go
Offered By: Gopher Academy via YouTube
Course Description
Overview
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
Related Courses
A Holistic Go ExperienceGopher Academy via YouTube Safe, Fast, and Easy - Building a Plugin System with WebAssembly
Gopher Academy via YouTube A Journey Through Integration Testing with Go - What Could Go Wrong?
Gopher Academy via YouTube Kraken Wrangling for Dummies - Go Module Dependency Management at Scale
Gopher Academy via YouTube Go, the Data Engineer's Missing Tool?
Gopher Academy via YouTube