Here comes type parameterization
Think of the same scenario. You were asked to write a program that contains some different entities, for example, humans and animals. Now, both require food to stay alive. Our colleagues are aware of this and have written code that takes care of the way food is served to humans as well as animals.
They have written the code and provided it as a library (we can access those functions by importing the packages they wrote). Our colleagues were ahead of time and wrote a function that looked like the following:
def servseMeal[A, B](serveTo: A, serveThis: Option[B]) = ???
We were told the function will work, we just have to provide who to serve the food to as the first parameter and an optional food item as a meal. The rest will be taken care of by the logic they've put. We tried a couple of ways and wrote a few applications of this as follows:
serveMeal(Human(Category("MALE")), None) serveMeal(Human(Category("FEMALE")), Some(Food())) serveMeal(Animal(), None) ...