Chapter 10. Advanced Functional Programming
"Like punning, programming is a play on words."
– Alan Peris
You're a developer, right? Think of a scenario where you're asked to write some software that has certain entities. Take a look at the following:
Animal | Food
We have the Animal
and Food
entities. We're working on an automated system that serves food to animals in a zoo. Suppose we're going to write a function which lets two animals share their food. It expects two animal objects, food, and then does the job. The function looks like this:
def serveSharedMeal( animalOne: Animal, animalTwo: Animal, meal: Food) = ??? //don't care about the implementation
So far, everything's fine, right? Let's introduce two animals now. We have two animal subclasses named Lion
and Deer
. Our simple function takes two animal
instances and shares the meal
instance among them. Now, you might wonder what happens when we pass instances of Lion
and Deer
; what might be the consequences?...