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

Rediscovering The Swift Compiler Through Closures

Rediscovering The Swift Compiler Through Closures

Keynote of the lecture given at iOS TLV Meetup on November 30 2016 by Aviel Gross

Swift compiler tries to create a world in which “If we understand what this is, then the compiler understands it too”. Type inference, type constraints, and method overloading are only some of the great things that help us write better and safer code, while helping the Swift compiler understand what we meant. In this talk we will hack through some of those functionalities and make Swift know what we meant before we even know it ourselves.

@avielgr

Aviel Gross

November 30, 2016
Tweet

More Decks by Aviel Gross

Other Decks in Programming

Transcript

  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

  6. Closures are self-contained blocks of functionality that can be passed

    around and used in your code. - Closure Chapter / The Swift Programming Language / Apple
  7. Solution A Our ‘weak(…)’ must: • Belong to ‘NotificationCenter’ •

    ✊ Take our object • % Return a weak ref’ed object
  8. Solution A ‘WithProxy’ should: • Keep ‘val’ type - generic

    • Pass our ‘val’ safely (weakly) • Have a proxy of ‘addObserver’
  9. Solution B That’s Nice But….. • A-lot of code! •

    ✌ We made 2 special extensions • + We made a brand new class! • Who is holding ‘WithProxy’?! We can do better…
  10. Solution C method overloading is the ability to create multiple

    methods of the same name with different implementations. Calls […] will run […] appropriate to the context of the call, […performing] different tasks depending on context - Wikipedia
  11. Solution C Method Overloading: • ”Same name” • . “Multiple

    methods” • 1 call - differentiates by context
  12. Solution E Solution A Solution B Solution C Solution D

    • Type Inference • Generics • Method Overloading • Closures