Protocols, Generics, and Existential Containers — Wait What?

The Existential Container

var vehicles: [Drivable]
1. payload_data_0 = 0x0000000000000004,
2. payload_data_1 = 0x0000000000000000,
3. payload_data_2 = 0x0000000000000000,
4. instance_type = 0x000000010d6dc408 ExistentialContainers`type
metadata for ExistentialContainers.Car,
5. protocol_witness_0 = 0x000000010d6dc1c0
ExistentialContainers`protocol witness table for
ExistentialContainers.Car : ExistentialContainers.Drivable
in ExistentialContainers
Structs adhering to the Drivable protocol
car = 
payload_data_0 = 0x0000000000000004,
payload_data_1 = 0x0000000000000000,
payload_data_2 = 0x0000000000000000,
instance_type = 0x000000010b50e410
ExistentialContainers`type metadata for
ExistentialContainers.Car,
protocol_witness_0 = 0x000000010b50e1c8
ExistentialContainers`protocol witness table for
ExistentialContainers.Car: ExistentialContainers.Drivable
in ExistentialContainers)
motorcycle =
payload_data_0 = 0x0000608000036820,
payload_data_1 = 0x0000000000000000,
payload_data_2 = 0x0000000000000000,
instance_type = 0x000000010b50e4d8
ExistentialContainers`type metadata for
ExistentialContainers.Motorcycle,
protocol_witness_0 = 0x000000010b50e1d8
ExistentialContainers`protocol witness table for
ExistentialContainers.Motorcycle:
ExistentialContainers.Drivable in ExistentialContainers
bus =
payload_data_0 = 0x00006000000364a0,
payload_data_1 = 0x0000000000000000,
payload_data_2 = 0x0000000000000000,
instance_type = 0x000000010b50e5a8
ExistentialContainers`type metadata for
ExistentialContainers.Bus,
protocol_witness_0 = 0x000000010b50e1e8
ExistentialContainers`protocol witness table for
ExistentialContainers.Bus: ExistentialContainers.Drivable
in ExistentialContainers

Function Parameters

Functions in question
func startTravelingWithCar(transportation: Car) { }
protocol Returnable {
associateType ReturnType
}
//This will compile
func returnTheType<T: Returnable>(object: T) -> T.ReturnType { } ✅
//This won't compile
func returnTheType(object: Returnable) -> object.ReturnType { } ❌

--

--

I’m Varindra, a math teacher turned iOS developer. I love math, animals, and making apps. Join me on my tech adventure!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store