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

Comparing Swift Programming Language for Generics Support

Comparing Swift Programming Language for Generics Support

Apart from my daily job at Zalando iOS team, i stumbled across a problem which then lead to Generics programming. I was trying to wrap UITableViewDelegate into a protocol Extension inspired by the Protocol Oriented Programming. I soon stumbled across another piece of puzzle. Static Dispatch vs Dynamic Dispatch. This is a topic i will write extensively sometime later. Then I was scanning the internet and reading research papers. I was reading this Advanced Swift and inside it somwhere was a link to this research papaer titled “Comparision of Programming Languages on the basis of support for Generics (Gracia 2003)”. The paper was extended in 2007. The gist is, it compared various programming lanagueges like Java, C++ and Haskell among others. Then i asked can i extend this research paper to include Swift?

It turned out, i along with my collegues help wrote the paper which shall be public sometime soon. And i made a talk for these concepts and presented to the entire mobile team on iOS guild.

For more details, chekck out http://kandelvijaya.com/?p=422

Vijaya Prakash Kandel

June 24, 2016
Tweet

More Decks by Vijaya Prakash Kandel

Other Decks in Programming

Transcript

  1. TEXT ▸ A Comparative Study of Language Support for Generic

    Programming (R Gracia 2003) ▸ An Extended Comparative Study of Language Support for Generic Programming (R Gracia 2007)
  2. AGENDA ▸ Refine Queue ▸ Concepts/Protocols ▸ Common errors and

    why? ▸ Protocol Oriented Programming (IF⏲ ) ▸ Finale: Swift vs Haskell in Generics
  3. INTRODUCTION GENERICS ▸ Grouping of similar functionality into a single

    Thing ▸ Parametric Polymorphism [not Just polymorphism] ▸ Compile Time / Static Polymorphism
  4. TEXT . ▸ NOT GENERIC ▸ Type Erasure Model ▸

    Heterogeneous set capable ▸ Objectie-C NSArray is somehow similar to this
  5. TEXT . ▸ Generic ▸ Type Safe ▸ Homogeneous set

    capable ▸ T==> type deduction from call site
  6. TEXT SO WHAT?? ▸ Capability can be extended beyond the

    authors intention ▸ Originates from Set theory. Highly useful for FPs.
  7. TEXT SO WHAT FOR US???? ▸ Swift uses Generics all

    over the Standard Library ▸ Swift has a great support for Generics ▸ Generics are awesome when all the <> brackets and T: U: make sense
  8. + CONCEPTS ▸ + means lhs and rhs can be

    added. It is a concept. ▸ In C++ are just documented. No language support. YUK
  9. TEXT HOW IT COMPILES? ▸ during compile time a concrete

    function is created in place of generic function. This is Separate Compilation. ▸ LLVM is smarter than this.
  10. TEXT TYPES ▸ A grouping of data structure ▸ Int,

    String, NavigationBarAppereance, classes.
  11. TEXT WHAT ARE NOT TYPES? ▸ Items that can not

    completely defined in isolation ▸ Don’t know what is the type of Self until conformed by some concrete Type
  12. TEXT WHAT ARE NOT TYPES? ▸ Items that can not

    completely defined in isolation ▸ Element is unknown unless conformed by somebody
  13. TEXT SO.. ▸ They are Generic placeholders (Incomplete Types) ▸

    so is Array, ▸ But not Array<Int>. Its a complete Type.
  14. TEXT THUS ▸ Protocols with Self requirement or associatedType requirement

    are not complete by themselves ▸ Thus, they cant be used like a Type ▸ They can be used to constrain other Types ▸ Examples: ▸ Queue<T> ▸ Array<T>
  15. TEXT 2. TYPE CONSTRAINTS Reduce function over a Collection Type

    only when the elements are SignedInteger
  16. TEXT NOTE: ▸ Make note that Swift provided “T:Comparable” which

    specifies T can be anything that conforms to a concept/ protocol Comparable. Whereas, T == Int would mean T must be a Integer type.
  17. TEXT TYPE ERASURE MODEL ▸ Generic Placeholder gives Type safety

    and feedback on code analysis ▸ Concrete Type parameter casts the provided argument into the Type method accepts. Hence, losing Type information. Type Erasure Polymorphism model or Not So Swift Model
  18. TEXT ARRAY OF PROTOCOL ▸ Array of Concrete Type cannot

    be implicitly converted to Array of Another Type ▸ But a single Concrete Type can be converted to
  19. ▸ Protocol are a special construct which occupies 40 bytes

    ▸ Int are 8 bytes on 64 bit machine ▸ Array<Int> == Array<8byte contagious memory> ▸ Array<AType> == Array<40Byte contagious memory> ▸ Swift wont transform :=> performance, memory, type loss TEXT WHY?
  20. TEXT MIXIN, DEFAULT IMPLEMENTATION ▸ DEMO ▸ Protocol Oriented Programming

    ▸ Avoid Subclassing ▸ Careful about: Static vs Dynamic Dispatch!!!!
  21. TEXT WHAT WE GOT! ▸ Protocol Oriented Programming ▸ We

    can subclass only from 1 parent. This makes multiple inheritance easy without actually inheriting ▸ Retroactive modeling (WHAT)
  22. TEXT RETROATTIVE MODELING ▸ Ability to add functionality after the

    Type has been defined completely. ▸ Even though Array is made by Swift Team we were able to extend it with useful function after 2 years finally.
  23. REFERENCES ▸ Programming". http://www.osl.iu.edu/publications/prints/2005/ garcia05:_extended_comparing05.pdf. N.p., 2016. Web. 22 May

    2016. ▸ "Generics In Swift, Part 2 · Episteme And Techne". Austinzheng.com. N.p., 2015. Web. 23 May 2016. ▸ Cook, Nate. "Are Swift Generics Separately Compiled?". Stackoverflow.com. N.p., 2016. Web. 24 May 2016. ▸ "Mixin". Wikipedia. N.p., 2016. Web. 24 May 2016. ▸ Tsai, Michael. "Michael Tsai - Blog - How Swift Implements Generics". Mjtsai.com. N.p., 2016. Web. 24 May 2016.