Last active
September 28, 2020 20:25
-
-
Save pjcau/e2cafde9539d6bc34a7fbeaf8a1274df to your computer and use it in GitHub Desktop.
Protocols inside protocols type with Equatable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
//inner Protocols type | |
protocol Aerodinamic : Equatable{ | |
var coeficient:Float { get set } | |
} | |
extension Aerodinamic { | |
static func ==(lhs: Self, rhs: Self) -> Bool { | |
return lhs.coeficient == rhs.coeficient | |
} | |
} | |
protocol Engine:Equatable { | |
var type:String { get set } | |
} | |
extension Engine { | |
static func ==(lhs: Self, rhs: Self) -> Bool { | |
return lhs.type == rhs.type | |
} | |
} | |
protocol Motor:Equatable { | |
associatedtype EngineType: Engine | |
var name:String { get set } | |
var engine: EngineType { get set } | |
} | |
extension Motor { | |
static func ==(lhs: Self, rhs: Self) -> Bool { | |
return lhs.name == rhs.name && lhs.engine == rhs.engine | |
} | |
} | |
//Outside Protocols type | |
protocol Automobile: Equatable { | |
associatedtype AerodinamicType: Aerodinamic | |
associatedtype EngineType: Motor | |
var wheels:Int { get set } | |
var passengers:Int { get set } | |
var aerodinamic: AerodinamicType { get set } | |
var engine: EngineType { get set } | |
} | |
extension Automobile { | |
static func ==(lhs: Self, rhs: Self) -> Bool { | |
return lhs.wheels == rhs.wheels && lhs.aerodinamic == rhs.aerodinamic && lhs.engine == rhs.engine | |
} | |
} | |
///concrete reals struct from protocols | |
struct EngineMotorType :Engine { | |
var type: String | |
} | |
struct MotorMobile: Motor { | |
var name: String | |
var engine: EngineMotorType | |
} | |
struct MobileAerodinamic: Aerodinamic { | |
var coeficient: Float | |
} | |
struct Fiat: Automobile { | |
var wheels: Int | |
var passengers: Int | |
var aerodinamic: MobileAerodinamic | |
var engine: MotorMobile | |
} | |
///create instances | |
let cinquecento = Fiat(wheels: 4, passengers: 2, aerodinamic: MobileAerodinamic(coeficient: 2.0), engine: MotorMobile(name: "Termic", engine: EngineMotorType(type:"2Cilindri"))) | |
let tipo = Fiat(wheels: 4, passengers: 5,aerodinamic:MobileAerodinamic(coeficient: 1.5),engine: MotorMobile(name: "Hibrid", engine:EngineMotorType(type:"3Cilindri"))) | |
///check | |
cinquecento == tipo ? print("they are equals") : print("they aren't equals") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment