ぜのぜ

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

39日目 Subscripts

日記

今日は酒を飲んで気持ちがいい

今日書いたコード

struct TimesTable {
    let multiplier: Int
    subscript(index: Int) -> Int {
        return multiplier * index
    }
}
let threeTimesTable = TimesTable(multiplier: 3)
print("six times three is \(threeTimesTable[6])")

struct Some {
    subscript(table: TimesTable) -> Int {
        return table.multiplier
    }
}
Some()[threeTimesTable]

初めて知ったこと

subscriptを初めて実装した.勝手にIntを使うものだと思ってたけど何でもいいらしい.