ABSTRACT

One of the things that you should have noticed by this point is that there are times when it can be helpful to group pieces of data together. We have seen two ways that we can do this. If all the data is the same type, you could use something like an Array or a List. The down side of this is that the compiler cannot check to make sure that you have the right number of elements. For example, if you want a point in 3-D with x, y, and z coordinates then you would need an Array[Double] that has three elements in it. Having more or less could cause the code to break and Scala would not be able to check for that until the program was running. The alternative, which also works if the types of the values are different is to use a tuple. That same 3-D point could be represented as a (Double, Double, Double). While this works reasonably well for a 3-D point, it too has some significant limitations. The main limitation is that being a (Double, Double, Double) does not tell you anything about what those three Doubles mean (i.e. which is x, which is y, and which is z) or how it should be used.