January 2020
Intermediate to advanced
532 pages
13h 31m
English
So far, we have only seen dispatch examples for methods that take a single argument. We can extend the same concept for multiple arguments, and that's simply called multiple dispatch.
So how does it work when multiple arguments are involved? Let's say we continue developing our space war game with the ability to detect collisions between different objects. To look at this in detail, we'll go through a sample implementation.
First, define functions that can check whether two rectangles overlap each other:
struct Rectangle top::Int left::Int bottom::Int right::Int # return two upper-left and lower-right points of the rectangle Rectangle(p::Position, s::Size) = new(p.y+s.height, p.x, p.y, p.x+s.width)end ...
Read now
Unlock full access