Let's say we're working with shapes, and their area and circumference.
struct Rect
height::Float64
width::Float64
end
r1 = Rect(6,4)
dump(r1)
Main.FD_SANDBOX_9378544065360897491.Rect
height: Float64 6.0
width: Float64 4.0
Then its easy to dispatch on these:
area(s::Rect) = s.height * s.width
circum(s::Rect) = 2*(s.height + s.width)
area(r1), circum(r1)
(24.0, 20.0)
AND it's easy to extend this from other downstream places where you may only need one or two of these functions:
struct Circle
r::Float64
end
c1 = Circle(3)
area(s::Circle) = π*s.r^2
circum(s::Circle) = 2π*s.r
area(c1), circum(c1)
(28.274333882308138, 18.84955592153876)
© Jerry Ling. Last modified: December 01, 2024. Powered by Franklin.jl.