External names for subscripts
As we mentioned earlier in this chapter, we can have multiple subscript signatures in our classes, structures, and enums. 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, but one will perform a multiplication operations, 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, we define two subscripts and each subscript is an integer type. The difference between the two subscripts is the external name within the definition...