ぜのぜ

しりとりしようぜのぜのぜのぜ

79日目

日記

昔使っていたMBAを引っ張り出してきたら古すぎてChromeの更新ができなかったりHomebrewのupgradeが全然終わらなかったりしてなかなかきびしい.

今日書いたコード

https://docs.swift.org/swift-book/LanguageGuide/Generics.html

protocol Container {
    associatedtype Item
    
    mutating func append(_ item: Item)
    var count: Int { get }
    subscript(i: Int) -> Item { get }
}

protocol SuffixableContainer: Container {
    associatedtype Suffix: SuffixableContainer where Suffix.Item == Item
    func suffix(_ size: Int) -> Suffix
}

extension Array: SuffixableContainer {
    func suffix(_ size: Int) -> [Element] {
        Self(self[(count - size)...])
    }
}

初めて知ったこと

associatedtypeに制約をつけられるというやつ.そのassociatedtypeが含まれるプロトコルを使うことができるので人類にはまだ早い感じがする.少なくとも俺は使いこなせる自信がない.