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 index: Int) -> Int { return num + index } }
As we can see, in this example we define two subscripts and each subscript is an integer type. The difference between...