KEMBAR78
Protocol oriented programming_talk_ppt | PPTX
Protocol Oriented Programming
Jayant Jaiswal, iOS Engineer, UrbanClap
#SwiftMeetup
What is it, in the first place ?
What is it, in the first place ?
Protocol Oriented Programming is a paradigm based around
the concept of Protocol Extensions.
Design a View Controller
Configurable Navigation Bar
Animation like show and hide loader
Loading from xib
A common approach
Design a base View Controller that gets inherited to all
those controllers that share the similarity.
A common approach
Design a base View Controller that gets inherited to all
those controllers that share the similarity.
This approach works but has
some drawbacks
A common approach
Design a base View Controller that gets inherited to all
those controllers that share the similarity.
This approach works but has
some drawbacks
Drawbacks
Drawbacks
Reusability, if same piece of code is required somewhere else
Drawbacks
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
Drawbacks
Inherit unneeded properties and methods, making the object become bloated
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
Drawbacks
Inherit unneeded properties and methods, making the object become bloated
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
It is said, OOP is just a
modularized way of writing
spaghetti code
Drawbacks
Inherit unneeded properties and methods, making the object become bloated
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
It is said, OOP is just a
modularized way of writing
spaghetti code
UIKit Framework in OOP
UIKit Framework in OOP
Would you ever like to work with the above code base?
Protocol Oriented Design to rescue
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Easier to debug and fix bugs
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Easier to debug and fix bugs
Works for both reference and value types - classes, enums and structs
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Easier to debug and fix bugs
Works for both reference and value types - classes, enums and structs
Let’s see them in action
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
A better solution
A better solution
A better solution
A better solution
A better solution
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
Add a functionality over Integers to convert them to string
Add a functionality over Integers to convert them to string
A common approach
Add a functionality over Integers to convert them to string
A common approach
Add a functionality over Integers to convert them to string
A common approach
Do the same for Float
Add a functionality over Integers to convert them to string
A common approach
Do the same for Float
Add a functionality over Integers to convert them to string
A common approach
Do the same for Float
A better approach using POP
A better approach using POP
A better approach using POP
A better approach using POP
A better approach using POP
A better approach using POP
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
Design a View Controller
Should be able to show a pop up
Should be able to configure right bar button item
The SuperClass Way
The SuperClass Way
Redesigning using Protocol Oriented Programming
Redesigning using Protocol Oriented Programming
Redesigning using Protocol Oriented Programming
Using the Newly designed Protocols
Using the Newly designed Protocols
Using the Newly designed Protocols
A simple example of how the Swift Team has used
Protocol Extensions
public protocol Sequence {
//Some come
public func map<T>(_ transform: (Self.Element) throws -> T ) rethrows -> [T]
}
extension Sequence {
public func map<T>(_ transform: (Self.Element) throws -> T ) rethrows -> [T] {
//Implementation by Swift Team
}
}
extension Array: Sequence { }
extension ArraySlice: Sequence { }
extension ReversedCollection: Sequence { }
Task 1
Calling the above method would result in 7 and 123a respectively
Problem:
Both the situations
are possible
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
Heterogenous Containers
Problem:
Both the situations
are possible
Heterogenous Containers
Heterogenous Containers
Heterogenous Containers
Heterogenous Containers
Constraint
Each element conforms to Summable
Homogeneous Containers
Homogeneous Containers
Homogeneous Containers
Advantages of Homogeneous over Heterogeneous
Containers
Advantages of Homogeneous over Heterogeneous
Containers
The tedious type check has been eliminated that we had to do in the
heterogenous case.
Advantages of Homogeneous over Heterogeneous
Containers
The tedious type check has been eliminated that we had to do in the
heterogenous case.
Eliminated the need to do dynamic time type checking and replaced it with safer
compile time type assertion.
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
Constraining Protocol Extensions With Types
Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
extension ReusableView {
public static var cellReuseIdentifier : String {
return String(describing: self)
}
}
Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
extension ReusableView {
public static var cellReuseIdentifier : String {
return String(describing: self)
}
}
Would the above extension really make any sense if we conform a String, Int
or any other type to this protocol apart from UIView ?
Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
extension ReusableView where Self : UIView {
public static var cellReuseIdentifier : String {
return String(describing: self)
}
}
How Swift Team used this trick in the Swift Standard library ?
How Swift Team used this trick in the Swift Standard library ?
extension CollectionType {
public func index(of element: Generator.Element ) -> Index? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
How Swift Team used this trick in the Swift Standard library ?
extension CollectionType {
public func index(of element: Generator.Element ) -> Index? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
Can elements of arbitrary collections be compared with == ?
How Swift Team used this trick in the Swift Standard library ?
extension CollectionType {
public func index(of element: Generator.Element ) -> Index? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
binary operator ‘==‘ cannot be applied to
two Generator.Element operands
How Swift Team used this trick in the Swift Standard library ?
extension CollectionType where Generator.Element : Equatable {
public func index(of element: Generator.Element ) -> Int? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
Task 2
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
protocol BasePower {
init( )
}
class Pokemon <Power : BasePower> {
func attack( ) -> Power {
return Power( )
}
}
Protocol Associated Types
protocol BasePower {
init( )
}
class Pokemon <Power : BasePower> {
func attack( ) -> Power {
return Power( )
}
}
Protocol Associated Types
Power Types
struct WaterPower : BasePower { }
struct FirePower : BasePower { }
struct LighteningPower : BasePower { }
Protocol Associated Types
class Pikachu : Pokemon<LighteningPower> { }
class Squirtle : Pokemon<WaterPower> { }
class Charizard : Pokemon<FirePower> { }
Protocol Associated Types
class Pikachu : Pokemon<LighteningPower> { }
class Squirtle : Pokemon<WaterPower> { }
class Charizard : Pokemon<FirePower> { }
let pikachu = Pikachu( )
pikachu.attack( )
let squirtle = Squirtle( )
squirtle.attack( )
let charizard = Charizard( )
charizard.attack( )
Protocol Associated Types
class Pikachu : Pokemon<LighteningPower> { }
class Squirtle : Pokemon<WaterPower> { }
class Charizard : Pokemon<FirePower> { }
let pikachu = Pikachu( )
pikachu.attack( )
let squirtle = Squirtle( )
squirtle.attack( )
let charizard = Charizard( )
charizard.attack( )
Problem:
Subclassing. It starts out with great
intentions, but eventually things get
a lot messier as exceptions arise.
protocol Pokemon {
associatedtype Power : BasePower
func attack( ) -> Power
}
Protocol Associated Types
extension Pokemon {
func attack( ) -> Power {
return Power( )
}
}
Protocol Associated Types
struct Pikachu : Pokemon {
typealias Power = LighteningPower
}
struct Squirtle : Pokemon {
// Here WaterPower is inferred as the associatedtype
func attack( ) -> WaterPower {
// custom attack logic
return WaterPower( )
}
}
Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
let pokemon: Pokemon
pokemon.attack( )
Type Erasures
let pokemon: Pokemon
pokemon.attack( )
Protocol Pokemon can only be used as a generic constraint
because it has self or associated type requirements
Type Erasures
let pokemon: Pokemon
pokemon.attack( )
Protocol Pokemon can only be used as a generic constraint
because it has self or associated type requirements
Type Erasures
Problem:
Pokemon is an abstract type, so we cannot instantiate it directly.
Type Erasures
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
Type Erasures
let p1 = AnyPokemon(Pikachu)
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
Type Erasures
let p1 = AnyPokemon(Pikachu)
let p2: AnyPokemon<FirePower>
p2 = AnyPokemon(Charizard)
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
Type Erasures
let p1 = AnyPokemon(Pikachu)
let p2: AnyPokemon<FirePower>
p2 = AnyPokemon(Charizard)
AnyPokemon<LighteningPower>
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
Type Erasures
let p1 = AnyPokemon(Pikachu)
let p2: AnyPokemon<FirePower>
p2 = AnyPokemon(Charizard)
AnyPokemon<LighteningPower>
AnyPokemon<FirePower>
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
Lets summarise…
How OOP can make things messier in the long run
How Protocol Oriented Design can overcome all the shortcomings
of OOPs
What are protocol extensions and how we can go about using them.
How we can go about converting from OOP to POP
Homogenous and Heterogenous Containers
Constraining extensions with types
Protocol Associated Types(PATs)
Type Erasures
Thank You

Protocol oriented programming_talk_ppt

  • 1.
    Protocol Oriented Programming JayantJaiswal, iOS Engineer, UrbanClap #SwiftMeetup
  • 2.
    What is it,in the first place ?
  • 3.
    What is it,in the first place ? Protocol Oriented Programming is a paradigm based around the concept of Protocol Extensions.
  • 4.
    Design a ViewController Configurable Navigation Bar Animation like show and hide loader Loading from xib
  • 5.
    A common approach Designa base View Controller that gets inherited to all those controllers that share the similarity.
  • 6.
    A common approach Designa base View Controller that gets inherited to all those controllers that share the similarity. This approach works but has some drawbacks
  • 7.
    A common approach Designa base View Controller that gets inherited to all those controllers that share the similarity. This approach works but has some drawbacks
  • 8.
  • 9.
    Drawbacks Reusability, if samepiece of code is required somewhere else
  • 10.
    Drawbacks Hard to navigatebetween classes and fix bugs, due to nested code Reusability, if same piece of code is required somewhere else
  • 11.
    Drawbacks Inherit unneeded propertiesand methods, making the object become bloated Hard to navigate between classes and fix bugs, due to nested code Reusability, if same piece of code is required somewhere else
  • 12.
    Drawbacks Inherit unneeded propertiesand methods, making the object become bloated Hard to navigate between classes and fix bugs, due to nested code Reusability, if same piece of code is required somewhere else It is said, OOP is just a modularized way of writing spaghetti code
  • 13.
    Drawbacks Inherit unneeded propertiesand methods, making the object become bloated Hard to navigate between classes and fix bugs, due to nested code Reusability, if same piece of code is required somewhere else It is said, OOP is just a modularized way of writing spaghetti code
  • 14.
  • 15.
    UIKit Framework inOOP Would you ever like to work with the above code base?
  • 16.
  • 17.
    Protocol Oriented Designto rescue Separated out functionalities making them reusable
  • 18.
    Protocol Oriented Designto rescue Separated out functionalities making them reusable Flat Structured, non-nested code
  • 19.
    Protocol Oriented Designto rescue Separated out functionalities making them reusable Flat Structured, non-nested code Easier to debug and fix bugs
  • 20.
    Protocol Oriented Designto rescue Separated out functionalities making them reusable Flat Structured, non-nested code Easier to debug and fix bugs Works for both reference and value types - classes, enums and structs
  • 21.
    Protocol Oriented Designto rescue Separated out functionalities making them reusable Flat Structured, non-nested code Easier to debug and fix bugs Works for both reference and value types - classes, enums and structs Let’s see them in action
  • 22.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 23.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 37.
    Add a functionalityover Integers to convert them to string
  • 38.
    Add a functionalityover Integers to convert them to string A common approach
  • 39.
    Add a functionalityover Integers to convert them to string A common approach
  • 40.
    Add a functionalityover Integers to convert them to string A common approach Do the same for Float
  • 41.
    Add a functionalityover Integers to convert them to string A common approach Do the same for Float
  • 42.
    Add a functionalityover Integers to convert them to string A common approach Do the same for Float
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 50.
    Design a ViewController Should be able to show a pop up Should be able to configure right bar button item
  • 51.
  • 52.
  • 53.
    Redesigning using ProtocolOriented Programming
  • 54.
    Redesigning using ProtocolOriented Programming
  • 55.
    Redesigning using ProtocolOriented Programming
  • 58.
    Using the Newlydesigned Protocols
  • 59.
    Using the Newlydesigned Protocols
  • 60.
    Using the Newlydesigned Protocols
  • 61.
    A simple exampleof how the Swift Team has used Protocol Extensions public protocol Sequence { //Some come public func map<T>(_ transform: (Self.Element) throws -> T ) rethrows -> [T] } extension Sequence { public func map<T>(_ transform: (Self.Element) throws -> T ) rethrows -> [T] { //Implementation by Swift Team } } extension Array: Sequence { } extension ArraySlice: Sequence { } extension ReversedCollection: Sequence { }
  • 62.
    Task 1 Calling theabove method would result in 7 and 123a respectively
  • 65.
  • 66.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
    Advantages of Homogeneousover Heterogeneous Containers
  • 76.
    Advantages of Homogeneousover Heterogeneous Containers The tedious type check has been eliminated that we had to do in the heterogenous case.
  • 77.
    Advantages of Homogeneousover Heterogeneous Containers The tedious type check has been eliminated that we had to do in the heterogenous case. Eliminated the need to do dynamic time type checking and replaced it with safer compile time type assertion.
  • 78.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 79.
  • 80.
    Constraining Protocol ExtensionsWith Types public protocol ReusableView : class { static var cellReuseIdentifier : String { get } }
  • 81.
    Constraining Protocol ExtensionsWith Types public protocol ReusableView : class { static var cellReuseIdentifier : String { get } } extension ReusableView { public static var cellReuseIdentifier : String { return String(describing: self) } }
  • 82.
    Constraining Protocol ExtensionsWith Types public protocol ReusableView : class { static var cellReuseIdentifier : String { get } } extension ReusableView { public static var cellReuseIdentifier : String { return String(describing: self) } } Would the above extension really make any sense if we conform a String, Int or any other type to this protocol apart from UIView ?
  • 83.
    Constraining Protocol ExtensionsWith Types public protocol ReusableView : class { static var cellReuseIdentifier : String { get } } extension ReusableView where Self : UIView { public static var cellReuseIdentifier : String { return String(describing: self) } }
  • 84.
    How Swift Teamused this trick in the Swift Standard library ?
  • 85.
    How Swift Teamused this trick in the Swift Standard library ? extension CollectionType { public func index(of element: Generator.Element ) -> Index? { for i in self.indices { if self[ i ] == element { return i } } return nil } }
  • 86.
    How Swift Teamused this trick in the Swift Standard library ? extension CollectionType { public func index(of element: Generator.Element ) -> Index? { for i in self.indices { if self[ i ] == element { return i } } return nil } } Can elements of arbitrary collections be compared with == ?
  • 87.
    How Swift Teamused this trick in the Swift Standard library ? extension CollectionType { public func index(of element: Generator.Element ) -> Index? { for i in self.indices { if self[ i ] == element { return i } } return nil } } binary operator ‘==‘ cannot be applied to two Generator.Element operands
  • 88.
    How Swift Teamused this trick in the Swift Standard library ? extension CollectionType where Generator.Element : Equatable { public func index(of element: Generator.Element ) -> Int? { for i in self.indices { if self[ i ] == element { return i } } return nil } }
  • 89.
  • 90.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 91.
    protocol BasePower { init() } class Pokemon <Power : BasePower> { func attack( ) -> Power { return Power( ) } } Protocol Associated Types
  • 92.
    protocol BasePower { init() } class Pokemon <Power : BasePower> { func attack( ) -> Power { return Power( ) } } Protocol Associated Types Power Types struct WaterPower : BasePower { } struct FirePower : BasePower { } struct LighteningPower : BasePower { }
  • 93.
    Protocol Associated Types classPikachu : Pokemon<LighteningPower> { } class Squirtle : Pokemon<WaterPower> { } class Charizard : Pokemon<FirePower> { }
  • 94.
    Protocol Associated Types classPikachu : Pokemon<LighteningPower> { } class Squirtle : Pokemon<WaterPower> { } class Charizard : Pokemon<FirePower> { } let pikachu = Pikachu( ) pikachu.attack( ) let squirtle = Squirtle( ) squirtle.attack( ) let charizard = Charizard( ) charizard.attack( )
  • 95.
    Protocol Associated Types classPikachu : Pokemon<LighteningPower> { } class Squirtle : Pokemon<WaterPower> { } class Charizard : Pokemon<FirePower> { } let pikachu = Pikachu( ) pikachu.attack( ) let squirtle = Squirtle( ) squirtle.attack( ) let charizard = Charizard( ) charizard.attack( ) Problem: Subclassing. It starts out with great intentions, but eventually things get a lot messier as exceptions arise.
  • 96.
    protocol Pokemon { associatedtypePower : BasePower func attack( ) -> Power } Protocol Associated Types extension Pokemon { func attack( ) -> Power { return Power( ) } }
  • 97.
    Protocol Associated Types structPikachu : Pokemon { typealias Power = LighteningPower } struct Squirtle : Pokemon { // Here WaterPower is inferred as the associatedtype func attack( ) -> WaterPower { // custom attack logic return WaterPower( ) } }
  • 98.
    Our Agenda MovingAhead What protocol Extensions are ? How to think the Protocol Extension Way ? How we can convert Object Oriented Design to its equivalent Protocol Oriented with a cleaner design and Reusability? Homogeneous and Heterogeneous Containers Constraining Protocol Extensions with types Protocol Associated Types(PATs) Type Erasures
  • 99.
  • 100.
    let pokemon: Pokemon pokemon.attack() Protocol Pokemon can only be used as a generic constraint because it has self or associated type requirements Type Erasures
  • 101.
    let pokemon: Pokemon pokemon.attack() Protocol Pokemon can only be used as a generic constraint because it has self or associated type requirements Type Erasures Problem: Pokemon is an abstract type, so we cannot instantiate it directly.
  • 102.
    Type Erasures class AnyPokemon<Power>: Pokemon { private let _attack: ( ) -> Power required init<U: Pokemon>(_ pokemon: Pokemon) where U.Power == Power { _attack = pokemon.attack } func attack( ) -> Power { _attack( ) } }
  • 103.
    Type Erasures let p1= AnyPokemon(Pikachu) class AnyPokemon <Power>: Pokemon { private let _attack: ( ) -> Power required init<U: Pokemon>(_ pokemon: Pokemon) where U.Power == Power { _attack = pokemon.attack } func attack( ) -> Power { _attack( ) } }
  • 104.
    Type Erasures let p1= AnyPokemon(Pikachu) let p2: AnyPokemon<FirePower> p2 = AnyPokemon(Charizard) class AnyPokemon <Power>: Pokemon { private let _attack: ( ) -> Power required init<U: Pokemon>(_ pokemon: Pokemon) where U.Power == Power { _attack = pokemon.attack } func attack( ) -> Power { _attack( ) } }
  • 105.
    Type Erasures let p1= AnyPokemon(Pikachu) let p2: AnyPokemon<FirePower> p2 = AnyPokemon(Charizard) AnyPokemon<LighteningPower> class AnyPokemon <Power>: Pokemon { private let _attack: ( ) -> Power required init<U: Pokemon>(_ pokemon: Pokemon) where U.Power == Power { _attack = pokemon.attack } func attack( ) -> Power { _attack( ) } }
  • 106.
    Type Erasures let p1= AnyPokemon(Pikachu) let p2: AnyPokemon<FirePower> p2 = AnyPokemon(Charizard) AnyPokemon<LighteningPower> AnyPokemon<FirePower> class AnyPokemon <Power>: Pokemon { private let _attack: ( ) -> Power required init<U: Pokemon>(_ pokemon: Pokemon) where U.Power == Power { _attack = pokemon.attack } func attack( ) -> Power { _attack( ) } }
  • 107.
    Lets summarise… How OOPcan make things messier in the long run How Protocol Oriented Design can overcome all the shortcomings of OOPs What are protocol extensions and how we can go about using them. How we can go about converting from OOP to POP Homogenous and Heterogenous Containers Constraining extensions with types Protocol Associated Types(PATs) Type Erasures
  • 108.