In this exercise you'll be implementing a simulation of encounters. This will familiarise you with the basics of multiple dispatch, Julia's main paradigm. Everything you might need to know can be found in the Methods chapter of the Julia Manual.
In general, encounters involve one party a
meeting another party b
and reacting to it.
encounter(a, b) = "$(name(a)) meets $(name(b)) and $(meets(a, b))."
At first, we will simulate what happens when cats and dogs meet.
Implement meets
methods for the following encounters:
- When two dogs meet, they sniff each other.
- When a cat meets a dog, it hisses at the dog.
- When a dog meets a cat, it chases the cat.
- When two cats meet, they slink.
But what happens if they encounter a different pet on their walk, like a horse?
First, add an abstract type Pet
to your implementation, of which Cat
and Dog
are subtypes.
- If a pet meets another pet that it doesn't recognize, it is cautious.
There are many other things that pets may encounter that aren't pets: cars, humans, plants, natural disasters, asteroids… What happens then?
- If a pet meets something it has never seen before, it runs away.
There are many other encounters that could occur in our simulation. A car meets a dog, two electrons encounter each other and interact, …. It is impossible to cover all encounters in advance, therefore we will implement a generic fallback.
- If two unknown things or beings encounter each other, nothing happens.