Typing Generic Go

Typing Generic Go

Gopher Academy via YouTube Direct link

Sort internals

11 of 24

11 of 24

Sort internals

Class Central Classrooms beta

YouTube playlists curated by Class Central.

Classroom Contents

Typing Generic Go

Automatically move to the next video in the Classroom when playback concludes

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

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.