- Julia 1.0 Programming Complete Reference Guide
- Ivo Balbaert Adrian Salceanu
- 324字
- 2021-06-24 14:21:50
Type unions
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.jl
mutable struct Point
x::Float64
y::Float64
end
mutable struct Vector2D
x::Float64
y::Float64
end
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 after defining the following, +(p, v) still returns the same error because of multiple dispatch. Julia has no way of knowing that +(p,v) should be the same as +(v,p):
+(p::Point, q::Point) = Point(p.x + q.x, p.y + q.y)
+(u::Vector2D, v::Vector2D) = Point(u.x + v.x, u.y + v.y)
+(u::Vector2D, p::Point) = Point(u.x + p.x, u.y + p.y)
Only when we define the type matching method as +(p::Point, v::Vector2D) = Point(p.x + v.x, p.y + v.y), do we get a result +(p, v), which returns Point(5.0,7.0).
Now you can ask the question: Don't multiple dispatch and many types give rise to code duplication, as is the case here?
The answer is no, because, in such a case, we can define a union type, VecOrPoint:
VecOrPoint = Union{Vector2D, Point}
If p is a point, it is also of type VecOrPoint, and the same is true for v which is Vector2D. isa(p, VecOrPoint) and isa(v, VecOrPoint); both return true.
Now we can define one + method that works for any of the preceding four cases:
+(u::VecOrPoint, v:: VecOrPoint) = VecOrPoint(u.x + v.x, u.y +
v.y)
So, now we only need one method instead of four.
- 精通Nginx(第2版)
- Maven Build Customization
- Docker進(jìn)階與實(shí)戰(zhàn)
- QGIS:Becoming a GIS Power User
- 你不知道的JavaScript(中卷)
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲(chóng)案例實(shí)戰(zhàn)全流程詳解(高級(jí)進(jìn)階篇)
- Swift Playgrounds少兒趣編程
- Haskell Data Analysis Cookbook
- Multithreading in C# 5.0 Cookbook
- Python機(jī)器學(xué)習(xí)之金融風(fēng)險(xiǎn)管理
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級(jí)賬本看區(qū)塊鏈架構(gòu)設(shè)計(jì)
- Python趣味編程與精彩實(shí)例
- H5+移動(dòng)營(yíng)銷(xiāo)設(shè)計(jì)寶典
- Python編程快速上手2
- 例說(shuō)FPGA:可直接用于工程項(xiàng)目的第一手經(jīng)驗(yàn)