61
GENERICS “0 TO 0”

Generics programming in Swift

Embed Size (px)

Citation preview

GENERICS“0 TO 0”

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

QUEUE

TEXT

QUEUE FOR INT

TEXT

QUEUE FOR STRING

TEXT

QUEUE FOR CGPOINT

TEXT

1000’S OF THESE

▸ Cant be extended

TEXT

WHAT IS CHANGING

TEXT

IDEA 1

TEXT

ANALYSE

▸ all value semantics conform to Any Protocol by default

▸ Is it Generic?

TEXT

.

▸ NOT GENERIC

▸ Type Erasure Model

▸ Heterogeneous set capable

▸ Objectie-C NSArray is somehow similar to this

TEXT

IDEA 2:

TEXT

.

▸ Generic

▸ Type Safe

▸ Homogeneous set capable

▸ T==> type deduction from call site

TEXT

TEXT

SO WHAT??

▸ Reduces redundancy

TEXT

SO WHAT??

▸ Type Safety

TEXT

1-….

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

TEXT

OOOH

TEXT

ADD!!

TYPELESS GENERICS

TEXT

PROPER TYPED GENERICS

+ is not defined on all types: unfortunately!!!

+

CONCEPTS

▸ + means lhs and rhs can be added. It is a concept.

▸ In C++ are just documented. No language support.

YUK

TEXT

CONCEPT == PROTOCOL

▸ Protocol to our rescue.

TEXT

SO……

▸ Expressive

▸ No Lies to the compiler

TEXT

FULL PICTURE

TEXT

FULL PICTURE

TEXT

FULL PICTURE

TEXT

AT CALL SITE

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

TYPES

▸ A grouping of data structure

▸ Int, String, NavigationBarAppereance, classes.

TEXT

TYPES

▸ Protocols that are complete by themselves are proper types

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

GENERICS VS LOOKS GENERICS

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

CODE

CODE

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

CODE

▸ 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

TEXT

TEXT

TEXT

AT THE CALL SITE

TEXT

AT CALL SITE

TEXT

ANOTHER CLASS

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.

TEXT

GENERICS SUPPORT FOR SWIFT

TEXT

Great Support For Generics

TEXT

ANY QUESTION?

👠

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.