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

Unleashing Swift :: #bristech 2015

Ddd6d3bac7772fa67fc5e312a18bdaec?s=47 sammyd
October 15, 2015

Unleashing Swift :: #bristech 2015

Just over a year ago, Apple launched a new programming language in the form of Swift: a fast​​, ​​safe​​ and ​​modern​​ pragmatic language following current ideas in language design that is shortly to be open-sourced. This talk will cover both the basics of the language as well discussion of the relevance of Swift to the non-Apple world within the context of new paradigms: protocol-oriented design and functional reactive programming.

Ddd6d3bac7772fa67fc5e312a18bdaec?s=128

sammyd

October 15, 2015
Tweet

Transcript

  1. Unleashing Swift

  2. @iwantmyrealname

  3. Choose your own Adventure

  4. swift

  5. fast modern safe interactive

  6. Strong typing Mutability Value Types ARC

  7. Some code var arr = [3, 4, 1, 6, 2,

    5]; arr.sort(); arr; //= ?? What's the langauge? and the result?
  8. Javascript var arr = [3, 4, 1, 6, 2, 5];

    arr.sort(); arr; //= [1, 2, 3, 4, 5, 6]
  9. Swift var arr = [3, 4, 1, 6, 2, 5];

    arr.sort(); arr; //= [3, 4, 1, 6, 2, 5]
  10. getting started

  11. Getting Started — Mutability — Value types — Classes /

    Structs — Functions — Extensions — Closures
  12. Mutability let twelve = 12 //twelve = 13 var thirteen

    = 13 thirteen = 12 //thirteen = 13.0
  13. Value Types var title = "This is a string" var

    secondTitle = title secondTitle += ", extended" print(title) // "This is a string" print(secondTitle) // "This is a string, extended"
  14. Classes — Reference types — Inheritance / Dynamic Dispatch class

    Vehicle { var maxSpeed: Double } class Car: Vehicle { var numberOfWheels: Int }
  15. Structs — Value types — No inheritance — Immutable by

    default struct Point3D { let x: Double let y: Double let x: Double }
  16. Class Initialisation 1. All stored properties 2. super.init() / self.init()

    3. Change inherited properties 4. Call instance methods / access self
  17. Functions func square(value: Int) -> Int { return value *

    value } func exp(value: Int, power: Int) -> Int { return power > 0 ? value * exp(value, power: power-1) : 1 } func curryPower(power: Int)(_ value: Int) -> Int { return exponentiate(value, power: power) } let cube = curryPower(3) cube(3) //= 27
  18. Extensions — Add functionality to types extension Int { func

    toThePower(power: Int) -> Int { return exponentiate(self, power: power) } } 2.toThePower(5)
  19. Closures typealias Callback = (reply: String) -> () func asyncHello(name:

    String, callback: Callback) { callback(reply: "Hello \(name)") }
  20. Functional Tinge — Functions are 1st class — map, reduce,

    filter on CollectionType — TLO implemented let scores = [1, 2, 4, 5, 3, 5] let cubedScores = scores.map { cube($0) } //= [1, 8, 64, 125, 27, 125]
  21. "modern" concepts

  22. Modern Concepts — Enums — Pattern Matching — Optionals —

    Protocols
  23. Enums — Raw values — Associated values — Equivalent to

    mapping || — Recursive enums enum Activity: String { case Running case Swimming case Cycling }
  24. Pattern Matching — Concept from functional languages — Part of

    if and switch func evaluate(expresion: ArithmeticExpression) -> Int { switch expresion { case .Number(let value): return value case .Addition(let left, let right): return evaluate(left) + evaluate(right) case .Multiplication(let left, let right): return evaluate(left) * evaluate(right) } }
  25. Optionals — Handling of nil — Just an enum with

    some syntactic sugar enum Optional<Wrapped> { case None case Some(Wrapped) }
  26. ?!??

  27. Protocols The problems with classes: — Inheritance — Implicit sharing

    — Lose type relationships
  28. Protocols (a.k.a. Interfaces) — Supports value types — Can add

    functionality to existing types — No instance data available — Can include default implementations
  29. Other Stuff

  30. Other Stuff — Interoperability — Tuples — Generics — Error

    Handling — Playgrounds
  31. the future

  32. Open source

  33. how can I play?

  34. buy a proper computer

  35. or wait

  36. you become a better programmer by playing with other lanugages

    — Me, just now
  37. go forth and play @iwantmyrealname github.com/sammyd