Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Embedding: It sure is weird

2,389 views

Published on

A talk all about Goland, Embedding, and the ways in which it works, and does not work.

Published in: Engineering
  • Be the first to comment

Embedding: It sure is weird

  1. 1. EMBEDDING IN GO IT SURE IS WEIRD @STABBYCUTYOU
  2. 2. OBLIGATORY OPENING SLIDES WHAT AM I HERE TO TALK ABOUT? ▸ Embedding! ▸ What is it, and how does it work? ▸ Useful ways to apply embedding for fun and profit!* ▸ *Neither fun nor profit guaranteed. ▸ A few ways you can’t use embedding, and why. ▸ Sweet pictures of my awesome dog!
  3. 3. GITHUB.COM/STABBYCUTYOU/EMBEDDINGTALK
  4. 4. EMBEDDING IT’S LIKE INHERITANCE, RIGHT?
  5. 5. EMBEDDING IS NOT BETTER THAN “CLASSICAL” INHERITANCE. IT’S A DIFFERENT TOOL FOR A DIFFERENT PROBLEM Me FUN QUOTE FOR YOU TO DISAGREE WITH
  6. 6. WHAT IS EMBEDDING? DEFINING EMBEDDING AND COMPOSITION ▸ Embedding is a way to re-use existing structs and interfaces via composition ▸ Think more behavior over lineage ▸ Embedding to define structure is perfectly valid, though ▸ Sideways Inheritance ▸ Embed to define the API of a struct or interface
  7. 7. WHAT IS EMBEDDING? HOW DOES IT DIFFER FROM “CLASSICAL” INHERITANCE? ▸ It does not set up or even imply a “base class / super class” relationship. ▸ “Is a” VS “Has a” style relationship ▸ Method dispatch mostly works as you think it would ▸ There are some interesting edge regarding “where” the method gets called from ▸ The embedding type cannot be exchanged for the embedded type ▸ You can’t pass in a Corgi to a method which wants a Dog
  8. 8. WHAT IS EMBEDDING? CLASSICAL INHERITANCE ▸ Kingdom - Animalia ▸ Phylum - Chordata ▸ Class - Mammalia ▸ Order - Carnivora ▸ Family - Canidae ▸ Genus - Canis ▸ Species - Canis Lupus ▸ Subspecies - Canis Lupus Familiaris ▸ Breed - Pembroke Welsh Corgi
  9. 9. WHAT IS EMBEDDING? EMBEDDING
  10. 10. WHAT IS EMBEDDING? EMBEDDING
  11. 11. WHAT IS EMBEDDING? WELL, WHAT DOES THE SPEC SAY? ▸ Not a whole lot! ▸ “A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct. An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.” ▸ “An interface T may use a (possibly qualified) interface type name E in place of a method specification. This is called embedding interface E in T; it adds all (exported and non- exported) methods of E to the interface T. An interface type T may not embed itself or any interface type that embeds T, recursively.” ▸ It defines a series of rules for determining how promotion works, but is light on examples.
  12. 12. WHAT IS EMBEDDING POINTER TYPES?
  13. 13. WHAT IS EMBEDDING POINTER TYPES!
  14. 14. WHAT IS EMBEDDING WHAT ABOUT…
  15. 15. WHAT IS EMBEDDING WHAT IF…
  16. 16. METHODSETS, SELECTORS, & PROMOTION OH MY
  17. 17. HOW DOES EMBEDDING WORK? SELECTORS ▸ Selectors are expressions that denote fields or methods available on an object ▸ “For a value x of type T or *T where T is not a pointer or interface type, x.f denotes the field or method at the shallowest depth in T where there is such an f. If there is not exactly one f with shallowest depth, the selector expression is illegal.” ▸ Example: t.F is a simple selector
  18. 18. HOW DOES EMBEDDING WORK? METHODSETS ▸ MethodSets are the collection of all available methods with a receiver of type T. ▸ For an interface, the interface is it’s own MethodSet ▸ Keyword: Set
  19. 19. HOW DOES EMBEDDING WORK? PROMOTION ▸ Promotion occurs when type E is embedded into type T, such that a field or method on E can be accessed at the shallowest depth of an instance of T. ▸ If E has field or method X, and T embeds E, X is promoted if T doesn’t already contain X ▸ Promoted methods and fields are called on their original receiver ▸ If T already contains X, X is called on T, never E
  20. 20. HOW DOES EMBEDDING WORK CREATING AND SETTING
  21. 21. PROMOTION COMMOTION EMBEDDING MULTIPLE STRUCTS ▸ You can embed more than one type into another type ▸ You can even nest embeddings ▸ You cannot embed the same type more than once ▸ What happens when you embed multiple types that result in ambiguous selectors? ▸ The compiler lets you do it! ▸ However, it does not let you invoke those selectors!
  22. 22. PROMOTION COMMOTION EMBEDDING MULTIPLE TYPES
  23. 23. PROMOTION COMMOTION EMBEDDING MULTIPLE TYPES
  24. 24. PROMOTION COMMOTION EMBEDDING EXPORTED TYPES ▸ You can embed any exported type from any package ▸ You cannot embed types from different packages with the same name ▸ What happens to un-exported fields and methods from exported types? ▸ There is a long standing “bug” in the reflect package about this! ▸ It’s currently managed via a comment in the standard lib ▸ “Broken as intended”
  25. 25. PROMOTION COMMOTION EMBEDDING INTERFACES IN STRUCTS ▸ You can do it ▸ Embed abstract behaviors instead of concrete behaviors ▸ Don’t panic! ▸ What about embedding 2 or more interfaces… ▸ With duplicate methods?
  26. 26. PROMOTION COMMOTION EMBEDDING INTERFACES IN STRUCTS
  27. 27. PROMOTION COMMOTION EMBEDDING INTERFACES IN STRUCTS
  28. 28. PROMOTION COMMOTION EMBEDDING INTERFACES IN STRUCTS
  29. 29. PROMOTION COMMOTION EMBEDDING INTERFACES IN STRUCTS
  30. 30. PROMOTION COMMOTION MARSHALING AND UNMARSHMALING ▸ You can expose and receive data for embedded types ▸ You can add tags to embedded types ▸ This changes the behavior of marshaling, be careful ▸ You can override and block out the marshaling behavior of embedded types ▸ Lots of examples, but little time to cover them all ▸ TL;DR;
  31. 31. WAYS I HAVE USED EMBEDDING FOR BETTER, OR WORSE
  32. 32. FUN EXAMPLE TIME VIEW MODELS ▸ Don’t send your database models over REST ▸ Don’t bind request data to them blindly either ▸ That’s a different talk ▸ Embed them in a ViewModel ▸ Use promotion semantics to hide private information ▸ Watch out for the marshal and unmarshal behaviors called out in the samples!
  33. 33. FUN EXAMPLE TIME VIEW MODELS
  34. 34. FUN EXAMPLE TIME EXTENDING GENERATED CODE ▸ If you produce generated code which contains type T ▸ You can product a write-once user-editable file which defines CustomT - (CT) ▸ Users can extend T however they see fit via CustomT and CustomT.go ▸ Re-generating the code should never touch CustomT.go, only T.go ▸ Unless the user wants to! ▸ Users can upgrade your code, while keeping their changes intact
  35. 35. NOT FUN EXAMPLE TIME REMEMBER: PROMOTED METHODS ARE CALLED ON THEIR ORIGINAL RECEIVER ▸ Here is a thing I got really really wrong ▸ Ported code from Ruby to Go ▸ Expected embedding to work similar to inheritance since “It’s better™” ▸ Didn’t ▸ If T embeds E, promoted method M1 will always call M2 on E ▸ Even if T includes method M2
  36. 36. SUMMARY THE LAST 40 SLIDES CONDENSED TO 3
  37. 37. ROUNDUP EMBEDDING ▸ It’s not better than inheritance ▸ It’s different ▸ The spec is light on details, examples ▸ Also refers to it by two names! ▸ Selectors and MethodSets are pretty simple ▸ Promotion is where it gets tricky ▸ You can embed interfaces, but it gets weird
  38. 38. ROUNDUP EMBEDDING ▸ Don’t touch the embedded type, only the embedding type ▸ Methods and Fields are always called relative to their original receiver ▸ Once you go down the embedded receiver chain, you can only go sideways, or further down ▸ Lots of ways to use embedding, not just as a replacement for inheritance
  39. 39. ROUNDUP EMBEDDING ▸ Once you embed a value, don’t touch it again ▸ Only interact with embedded types via the promotion exposed by their embedding type ▸ It’s too easy to inadvertently use the wrong value otherwise ▸ Or, throw caution to the wind ▸ I’m not the Go police
  40. 40. THANKS! @STABBYCUTYOU

×