External names for subscripts
As we mentioned earlier in this chapter, we can have multiple subscript signatures for our custom types. The appropriate subscript will be chosen, based on the type of index passed into the subscript. There are times when we may wish to define multiple subscripts that have the same type. For this, we could use external names similar to how we define external names for the parameters of a function.
Let's rewrite the original MathTable
structure to include two subscripts that each accept an integer as the subscript
type; however, one will perform a multiplication operation, and the other will perform an addition operation:
struct MathTable { var num: Int subscript(multiply index: Int) -> Int { return num * index } subscript(addition ...
Get Mastering Swift 3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.