Quickly expanding an example array in a playground as intended
protocol Vehicle { } class Car : Vehicle { } class Boat: Vehicle { } var carArray = [Car]() var vehicleArray : [Vehicle] = carArray as [Vehicle] vehicleArray.append(Car()) // [__lldb_expr_183.Car] vehicleArray.append(Boat()) // [__lldb_expr_183.Car, __lldb_expr_183.Boat]
I didnโt work too much with fast generics, but quickly looked at quick documents.
struct Stack<T: Vehicle> { var items = [Vehicle]() mutating func push(item: Vehicle) { items.append(item) } mutating func pop() -> Vehicle { return items.removeLast() } }
and the use of vehicles instead of the general type T
var vehicleStack = Stack<Vehicle>() vehicleStack.push(Car()) vehicleStack.push(Boat()) var aVehicle = vehicleStack.pop()
appears to compile aok in the application (there are some problems with its processing on the playground)
source share