-
Notifications
You must be signed in to change notification settings - Fork 2
/
shapes.jl
56 lines (42 loc) · 1.12 KB
/
shapes.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Shape
abstract type T end
area(shape::T) = throw(MethodError(area, shape))
combined_area(a::T, b::T) = area(a) + area(b)
end
module Circle
import ..Shape
struct T <: Shape.T
diameter::Float64
end
radius(c::T) = c.diameter / 2
Shape.area(c::T) = π * radius(c) ^ 2
end
module AbstractRectangle
import ..Shape
abstract type T <: Shape.T end
width(rectangle::T) = throw(MethodError(width, rectangle))
height(rectangle::T) = throw(MethodError(width, rectangle))
Shape.area(r::T) = width(r) * height(r)
end
module Rectangle
import ..AbstractRectangle
struct T <: AbstractRectangle.T
width::Float64
height::Float64
end
AbstractRectangle.width(r::T) = r.width
AbstractRectangle.height(r::T) = r.height
end
module Square
import ..AbstractRectangle
struct T <: AbstractRectangle.T
length::Float64
end
AbstractRectangle.width(s::T) = s.length
AbstractRectangle.height(s::T) = s.length
end
c = Circle.T(3)
s = Square.T(3)
r = Rectangle.T(3, 2)
@show Shape.combined_area(c, s)
@show Shape.combined_area(s, r)