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

Fun fact about Swift

Sash Zats
November 23, 2014

Fun fact about Swift

Got an invitation to a swift cocktail party? Don't worry, these flashcards will help you to keep up with conversations around you (most of it is probably invalid by the time you read it or won;t even compile)

Sash Zats

November 23, 2014
Tweet

More Decks by Sash Zats

Other Decks in Technology

Transcript

  1. Swift flashcards
    A guide to success at
    cocktail parties
    Sash Zats - zats.io - @zats - September 8, Tel Aviv

    View full-size slide

  2. [self helloAgain:AAPLAudienceTypeSwiftLovers];

    View full-size slide

  3. [self helloAgain:AudienceTypeSwiftLovers];

    View full-size slide

  4. self.helloAgain(AudienceTypeSwiftLovers)

    View full-size slide

  5. self.helloAgain(.SwiftLovers)

    View full-size slide

  6. Design
    • Small runtime (~5Mb? Pff), deployable to the previous OSes
    • Statically compiled, no JIT, garbage collection… duh…
    • No legacy, no abstraction penalties: Ints & Floats are
    structs
    • ARC
    • Multithreading built into language (aka atomic). NO!

    View full-size slide

  7. KVO-ish
    Take action while property setter is in flight
    var pet: Pet {
    willSet (newPet) {
    // cleanup
    }
    didSet {
    // awesome
    }
    }

    View full-size slide

  8. Functions overloading
    Same function name, different signatures
    func add(a: Int, b: Int) -> Int
    func add(a: String, b: String) -> String

    View full-size slide

  9. Operator overloading

    View full-size slide

  10. Typed collections
    var bag: Cat[] = [ snowball, oliver, jasper ]
    bag.append(scoobyDoo) // compiler error
    var mapping: [Bool: String] = Dictionary()
    mapping[true] = "true"
    mapping[true]!.utf8 // don't forget to unwrap
    mapping[false] = 1 // compiler error

    View full-size slide

  11. Optional chaining
    I don't always unwrap, but when I do…
    let y: SomeClass? = nil
    let z = y?.someMethod() // will produce nil

    View full-size slide

  12. Mutable, immutable collections
    var strings: [String] = [ "a", "b", "c" ]
    strings.append("d")
    let iStrings = string
    iStrings.append("e") // compiler error
    let iInts: [Int] = [ 1, 2, 3 ]
    var ints = iInts
    ints.append("d") // hmmm…

    View full-size slide

  13. siwtch statments
    switch size {
    case "a", "b":
    println("Small")
    case "c"..."e":
    println("Medium")
    case "f"..<"e":
    println("Large")
    default:
    println("Run!!")
    }

    View full-size slide

  14. Syntactic sweetness
    • Trailing closures
    dispatch_async(queue) {
    println("dispatch!")
    }
    • Freaking emoji in variables!
    let !" = "Moof"
    No emoji in operators !

    View full-size slide

  15. Optional Bools
    var b: Bool?
    if let b = b {
    if (b) {
    println("YES")
    } else {
    println("NO")
    }
    } else {
    println("Don't know")
    }

    View full-size slide

  16. Runtime
    • Compatible Mach-O binaries, ≈ Objective C
    • No dynamic lookup: virtual tables (isa → isa → …
    SwiftObject ), "Protocol witness table"
    • Devirtualization: no subclasses, @final
    • Meta programming only through @objc (no Mantle, no
    swizzling)
    • struct's functions

    View full-size slide

  17. Uniqueness
    • Modules
    • Name mangling
    • MyClass & Model gone wild
    • LLDB can use modules instead of DWARF to understand
    types, including "not included" generics!
    • xcrun swift-demangle _TF5MyApp6myFuncFTSiSi_TSS_ !
    MyApp.myFunc(Int, Int) -> (String)

    View full-size slide

  18. Thanks
    func memoize( body: ((T)->U, T)->U ) -> (T)->U {
    var memo = Dictionary()
    var result: ((T)->U)!
    result = { x in
    if let q = memo[x] { return q }
    let r = body(result, x)
    memo[x] = r
    return r
    }
    return result
    }
    let factorial = memoize { factorial, x in x == 0 ? 1 : x * factorial(x - 1) }

    View full-size slide

  19. Stop reading NSHipster,
    all the cool kids are at dev forums!

    View full-size slide