zoukankan      html  css  js  c++  java
  • swift的泛型貌似还差点意思

    protocol Container {
        typealias ItemType
        mutating func append(item: ItemType)
        mutating func removelast() -> ItemType
        var count: Int {get}
        subscript(i: Int) -> ItemType{get}
    
    }
    
    // Container<T> ???
    // protocol not gen!
    struct Hole<T>: Container {
        typealias ItemType = T
        var elements = ItemType[]()
        var count: Int {
        get{
            return elements.count
        }
        }
        mutating func append(item: ItemType){
            elements.append(item)
        }
        subscript(i: Int) -> ItemType {
            return elements[i]
        }
        mutating func removelast() -> ItemType{
            return elements.removeLast()
        }
    }
    
    class Stack<T>{
        var storage = Hole<T>()
        typealias ItemType = T
        typealias StorageType = Hole<T>.ItemType
        func push(item:ItemType) {
            storage.append(item)
        }
        func pop() -> ItemType{
            return storage.removelast()
        }
    }

    protocol不支持泛型,带来很多限制,不够灵活, 难道是为了兼容objc?

    如果可以这么用,就会更灵活

    class Stack<T, T2:Container>{
        var storage = T2<T>()
        ....
    }
  • 相关阅读:
    视图集
    子类视图
    Mixin扩展类
    GenericAPIView
    APIView
    ModelSerializer使用
    序列化和反序列化
    合并购物车
    pyplot基本绘制
    STL sort “invalid operator <”
  • 原文地址:https://www.cnblogs.com/lightlfyan/p/3768353.html
Copyright © 2011-2022 走看看