1 min read
Demo of types in Julia

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)

\show{./code/type_demo}

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)

\show{./code/type_demo}

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) =*s.r
area(c1), circum(c1)

\show{./code/type_demo}