Swift Generics & Upcasting

后端 未结 1 789
野性不改
野性不改 2020-12-03 09:09

I\'ve got a quick question regarding generics in Swift. The problem is I\'m trying to store a variable that takes a generic as a parameter, but am unable to cast it up to th

相关标签:
1条回答
  • 2020-12-03 09:36

    Swift generics are not covariant. That is to say, exactly what the error says: you can't automatically say a Basket<Apple> is a kind of Basket<Fruit> even if Apple is a kind of Fruit. There is good reason for this.

    Consider the following code:

    class Fruit {}
    class Apple: Fruit {}
    class Orange: Fruit {}
    
    class Basket<T: Fruit> {
        private var items: [T]
        func add(item: T) {
            items.append(item)
        }
        init() {}
    }
    
    func addItem<T: Fruit>(var basket: Basket<T>, item: T) {
        basket.add(item)
    }
    
    let basket:Basket<Apple> = Basket()
    
    addItem(basket as Basket<Fruit>, Orange())
    

    This would be legal code if Basket<Apple> were considered a Basket<Fruit>, and I'd be allowed to add an orange to a basket of apples.

    0 讨论(0)
提交回复
热议问题