In geometry, a two-dimensional point and a vector are not the same, even if they both have an x and y component. In Julia, we can also define them as different types, as follows:
# see the code in Chapter 6\unions.jlmutable struct Point x::Float64 y::Float64endmutable struct Vector2D x::Float64 y::Float64end
Here are the two objects:
-
p = Point(2, 5) that returns Point(2.0, 5.0)
-
v = Vector2D(3, 2) that returns Vector2D(3.0, 2.0)
Suppose we want to define the sum for these types as a point which has coordinates as the sum of the corresponding coordinates:
+(p, v)
This results in an ERROR: MethodError: `+` has no method matching +(::Point, ::Vector2D) error message.
To define a + method here, first do an import Base.+
Even ...