good morning swift hi
play

Good Morning SWIFT HI! I'm Marc Prud'hommeaux marc@glimpse.io - PowerPoint PPT Presentation

Good Morning SWIFT HI! I'm Marc Prud'hommeaux marc@glimpse.io Swift Public beta: June 2014 v1.0: September 2014 v1.2: February 2015 POLL: Swift? Agenda 1. Swift Syntax 2. Interesting Stuff 3. Questions (GOTO Guide) // Swift Bank &


  1. Immutability in Java Animal a = new Animal("dog"); Animal b = a; b.type; // "dog" a.type = "cow"; b.type; // "cow"

  2. Immutability in Java final Animal a = new Animal("dog"); final Animal b = a; b.type; // "dog" a.type = "cow"; b.type; // "cow"

  3. final Animal a = new Animal("dog"); final Animal b = a; b.type; // "dog" a.type = "cow"; b.type; // "cow" b = new Animal("moose"); // illegal!

  4. Reference vs. Value Types class Animal { var type: String } let a = new Animal("dog") let b = a b.type // "dog" a.type = "cow" b.type // "cow"

  5. Reference vs. Value Types struct Animal { var type: String } var a = new Animal("dog") var b = a b.type // "dog" a.type = "cow" b.type // "dog" <-------

  6. Reference vs. Value Types Similarities

  7. Reference vs. Value Types Similarities • contain properties

  8. Reference vs. Value Types Similarities • contain properties • contain methods

  9. Reference vs. Value Types Similarities • contain properties • contain methods • contain initializers

  10. Reference vs. Value Types Similarities • contain properties • contain methods • contain initializers • conform to protocols

  11. Reference vs. Value Types Differences • cannot inherit

  12. Reference vs. Value Types Differences • cannot inherit • no identity

  13. Reference vs. Value Types Differences • cannot inherit • no identity • unshared • immutable

  14. struct Animal { var type: String } var a = new Animal("dog") a.type = "cow"

  15. struct Animal { var type: String } var a = new Animal("dog") var b = a b.type // "dog" a.type = "cow" b.type // "dog" <-------

  16. struct Bank { var accounts: [Account] = [] var holdings: Int { return accounts.map({ $0.pennies }).reduce(0, combine: +) } } struct Account { var owner: String var pennies: Int = 0 } var bank = Bank() var a1 = Account(owner: "Marc", pennies: 100) bank.accounts += [a1] var a2 = Account(owner: "Dave", pennies: 1_000) bank.accounts += [a2] bank.holdings // => 1,100 bank.accounts[0].pennies -= 50 bank.holdings // => 1,050

  17. struct Bank { var accounts: [Account] = [] var holdings: Int { return accounts.map({ $0.pennies }).reduce(0, combine: +) } } struct Account { var owner: String var pennies: Int = 0 } var bank = Bank() var a1 = Account(owner: "Marc", pennies: 100) bank.accounts += [a1] var a2 = Account(owner: "Dave", pennies: 1_000) bank.accounts += [a2] bank.holdings // => 1,100 bank.accounts[0].pennies -= 50 bank.holdings // => 1,050 a1.pennies -= 50 bank.holdings // => 1,050 // !?!

  18. Deceptive!

  19. But convenient

  20. Strings in Java: pretend references

  21. Strings in Swift: true references

  22. Values all the way down

  23. So What?

  24. Benefits

  25. Benefits 1. comprehensible

  26. Benefits 1. comprehensible 2. testable

  27. Benefits 1. comprehensible 2. testable 3. parallel

  28. Benefits 1. comprehensible 2. testable 3. parallel 4. portable

  29. Not 100%

  30. Mac & iOS are built on reference types

  31. Your App • X% references • Y% values

  32. Model

  33. Benefits 1. comprehensible 2. testable 3. parallel 4. portable

  34. comprehensible

  35. testable

  36. parallel

  37. portable

  38. The Value of Values 1. comprehensible 2. testable 3. parallel 4. portable

  39. Questions?

Recommend


More recommend