Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Recipe for Value SEMANTICS (not value types!)

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
October 26, 2016

A Recipe for Value SEMANTICS (not value types!)

Who can forget 2015, The Year of the Value Type? Through numerous blogs and videos, the Swift community explored how value types (structs and enums) enable new, simpler, safer patterns for application architecture. This talk will argue for an important proviso: what a lot of these talks are trying to talk about is not value types, but value *semantics*. Value semantics is tricky to define but the reward is a profound satori-like experience of enlightenment, and a better understanding of how to use Swift.

This talk will explain value semantics, and provide a straightforward recipe for enforcing value semantics in Swift using value types, reference types, or a mixture of the two.

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

October 26, 2016
Tweet

Transcript

  1. a recipe for Value SEMANTICS (not value types!) @alexisgallagher

  2. POP SURVEY!

  3. Agenda • review it • a proposed definition of it

    • recipe for it
  4. REVIEW IT

  5. The Mutation Game • Victor the Valucist: • chooses a

    type Foo • defends value of variable v • Salazar the SideEffector: • defines attack statement, using variable s • defines pure audit function valueOf
  6. The Mutation Game Players: Victor the Valucist vs Salazar the

    SideEffector. Arena: var v:Foo = Foo() // Valucist chooses Foo to var s = v // defend the variable v let v0 = valueOf(v) /* { SideEffector attacks v using only s } */ let v1 = valueOf(v) assert(v0 == v1) // v unchanged? Valucist wins!
  7. None
  8. Valucist wins with Int var v:Int = 100 var s

    = v let v0 = valueOf(v) s = 200 // attempted attack! let v1 = valueOf(v) assert(v0 == v1) // but true, Valuecist wins!
  9. None
  10. SideEffector wins with NSMutableString var v:NSMutableString = NSMutableString() var s

    = v let v0 = valueOf(v) s.append("Hello, world") // attempted attack! let v1 = valueOf(v) assert(v0 == v1) // false! SideEffector wins
  11. None
  12. So what? Benefits of value types • prevent unintended mutation

    • makes reasoning easier and more local • makes functions great again • helps with thread safety
  13. Many great talks on value types ! Building Better Apps

    with Value Types, Gregor & Dudney, wwdc2015/s414; ! Making Friends with Value Types, Matuschak (2015), Realm; ! Protocol-Oriented Programming, Abrahams, wwdc2015/s408; ! What’s New in Foundation for Swift, Parker & LeHew, wwdc2016/s207; ! Value of Values. Rich Hickey (2012), InfoQ; ! Structs or Class. Crawford (2016); ! When to Use Swift Structs and Classes. Ash (2015). Value Semantics and Concepts-based Polymorphism Sean Parent (2012), BoostCon.
  14. structs vs classes?

  15. Does SideEffector win with classes? • No. Consider immutable classes.

    var v:UIImage = UIImage(named:"smile.jpg") var s = v let v0 = valueOf(v) // Hammer on s all you please. It's useless! let v1 = valueOf(v) assert(v0 == v1)
  16. None
  17. Does Valucist win with structs? • No. Consider structs containing

    classes: var v:Array<NSMutableString> = [NSMutableString()] var s = v let v0 = valueOf(v) s[0].append("Hello, world") let v1 = valueOf(v) assert(v0 == v1) // false, SideEffector wins
  18. None
  19. None
  20. None
  21. None
  22. Q: So which types win the mutation game?

  23. Q: So which types win the mutation game? A: Types

    with value semantics
  24. DEFINE IT

  25. DEFINE IT beyond saying "like a value type" except ...

  26. The Mutation Game is the defining test of value semantics

    A type Foo has value semantics if the Valucist always wins: var v:Foo = Foo() // Valucist chooses Foo to defend v var s = v let v0 = valueOf(v) /* { SideEffector attacks v using only s } */ let v1 = valueOf(v) assert(v0 == v1) // v unchanged? Valucist wins!
  27. conceptual definition of value semantics Value semantics is a guarantee

    of the independence of values of variables. A type has value semantics if the only way to modify a variable's value is through the variable itself. (where a variable's value is just the value of its instance, and an instance's value is defined as if by Equatable.)
  28. Adapts hints of Apple's definition ...

  29. ... to emphasize effects and use

  30. value semantics is INTERFACE value type is IMPLEMENTATION

  31. value semantics lets you dream in values

  32. None
  33. None
  34. None
  35. None
  36. Consequence 1: Immutable Reference Types have Value Semantics !

  37. Consequence 2: Types have Value Semantics relative to an access

    level
  38. None
  39. RECIPE FOR value semantic types

  40. Recipe for Value Semantic Types • primitive value type →

    sit tight They provide value semantics by default. • reference type → make it immutable Use let-constant stored properties of value semantic types. • composite value type → make a choice • either, use stored properties all of value semantic types • or, handle the tricky copy-on-write case
  41. Recipe for Value Semantic Types The tricky copy-on-write case 1.

    Pick the value semantic access level for the type's users 2. Restrict any mutable reference-type properties to a lower acces level 3. Define accessible setters and mutating functions to copy the mutable instance instead of mutating a shared instance 4. Verify it passes the Mutation Game Test.
  42. None
  43. SURVEY ANSWERS

  44. One consistent survey response: 1. Are structs value types? Yes,

    definitionally, since they use assign-by-(shallow)-copy. 2. Do structs have value semantics? No. But yes when designed to pass the mutation game test. 3. Are classes value types? No, by definition, as they use assign-by-reference. 4. Do class have value semantics? No. But yes when immutable.
  45. Key points (my take?) on value semantics • definable as:

    only a variable can affect its own value • testable by: the Mutation Game • can be built by recipe, from value or reference types • defined relative to access level • is about interface
  46. Acknowledgements Thanks for informal discussion and manuscript criticisms: • Chris

    Belanger, Russ Bishop, Alan Cannistraro, Chris Eidhof, Benjamin Encz, Tyler Fox, Steven Van Impe, Andy Matuschak, Jordan Rose All errors are where I did not listen.
  47. a recipe for Value SEMANTICS (not value types!) @alexisgallagher