generics programming in swift
TRANSCRIPT
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)
AGENDA
▸ Refine Queue
▸ Concepts/Protocols
▸ Common errors and why?
▸ Protocol Oriented Programming (IF⏲ )
▸ Finale: Swift vs Haskell in Generics
INTRODUCTION
GENERICS
▸ Grouping of similar functionality into a single Thing
▸ Parametric Polymorphism [not Just polymorphism]
▸ Compile Time / Static Polymorphism
TEXT
.
▸ NOT GENERIC
▸ Type Erasure Model
▸ Heterogeneous set capable
▸ Objectie-C NSArray is somehow similar to this
TEXT
SO WHAT??
▸ Capability can be extended beyond the authors intention
▸ Originates from Set theory. Highly useful for FPs.
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
+
CONCEPTS
▸ + means lhs and rhs can be added. It is a concept.
▸ In C++ are just documented. No language support.
YUK
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.
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
TEXT
WHAT ARE NOT TYPES?
▸ Items that can not completely defined in isolation
▸ Element is unknown unless conformed by somebody
TEXT
SO..
▸ They are Generic placeholders (Incomplete Types)
▸ so is Array,
▸ But not Array<Int>. Its a complete Type.
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>
TEXT
2. TYPE CONSTRAINTS
Reduce function over a Collection Type only when the elements are SignedInteger
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.
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
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
▸ 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?
TEXT
MIXIN, DEFAULT IMPLEMENTATION
▸ DEMO
▸ Protocol Oriented Programming
▸ Avoid Subclassing
▸ Careful about: Static vs Dynamic Dispatch!!!!
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)
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.
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.