August 2018
Intermediate to advanced
380 pages
10h 2m
English
Consider that we have the following list:
val list: List[Any] = List(0, 2.0, "3", Fraction(4, 2))
So, in the preceding list, we have elements of different types, and hence we are forced to declare this list as List[Any]. Fraction is defined for the purpose of our example as follows:
case class Fraction(numerator: Int, denominator: Int)
Notice that in the preceding list, each element can be represented as a floating-point number. They have different types, but it is possible to define certain common behavior on all of these types. Since the elements are very similar to each other, we might want to perform a common operation on them. For example, we may want to find the sum of all the numbers ...