$30 off During Our Annual Pro Sale. View Details »

A recipe for value SEMANTICS (not value types!)

A recipe for value SEMANTICS (not value types!)

This talk about the importance of value semantics (not to be confused with value types) in Swift.

Presented at Realm on October 26, 2016.

video: https://academy.realm.io/posts/swift-gallagher-value-semantics/

Alexis Gallagher

October 26, 2016
Tweet

More Decks by Alexis Gallagher

Other Decks in Technology

Transcript

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

    View Slide

  2. POP
    SURVEY!

    View Slide

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

    View Slide

  4. REVIEW IT

    View Slide

  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

    View Slide

  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!

    View Slide

  7. View Slide

  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!

    View Slide

  9. View Slide

  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

    View Slide

  11. View Slide

  12. So what? Benefits of value types
    • prevent unintended mutation
    • makes reasoning easier and more local
    • makes functions great again
    • helps with thread safety

    View Slide

  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.

    View Slide

  14. structs vs classes?

    View Slide

  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)

    View Slide

  16. View Slide

  17. Does Valucist win with structs?
    • No. Consider structs containing classes:
    var v:Array = [NSMutableString()]
    var s = v
    let v0 = valueOf(v)
    s[0].append("Hello, world")
    let v1 = valueOf(v)
    assert(v0 == v1) // false, SideEffector wins

    View Slide

  18. View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. Q: So which types win the
    mutation game?

    View Slide

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

    View Slide

  24. DEFINE IT

    View Slide

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

    View Slide

  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!

    View Slide

  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.)

    View Slide

  28. Adapts hints of Apple's definition ...

    View Slide

  29. ... to emphasize effects and use

    View Slide

  30. value semantics is
    INTERFACE
    value type is
    IMPLEMENTATION

    View Slide

  31. value semantics
    lets you dream
    in values

    View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. View Slide

  36. Consequence 1:
    Immutable Reference Types
    have Value Semantics
    !

    View Slide

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

    View Slide

  38. View Slide

  39. RECIPE FOR
    value semantic types

    View Slide

  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

    View Slide

  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.

    View Slide

  42. View Slide

  43. SURVEY
    ANSWERS

    View Slide

  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.

    View Slide

  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

    View Slide

  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.

    View Slide

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

    View Slide