- Julia 1.0 Programming Complete Reference Guide
- Ivo Balbaert Adrian Salceanu
- 418字
- 2021-06-24 14:21:51
Parametric types and methods
An array can take elements of different types. Therefore, we can have, for example, arrays of the following types: Array{Int64,1}, Array{Int8,1}, Array{Float64,1}, or Array{String, 1}, and so on. That is why an Array is a parametric type; its elements can be of any arbitrary type T, written as Array{T, 1}.
In general, types can take type parameters, so that type declarations actually introduce a whole family of new types. Returning to the Point example of the previous section, we can generalize it to the following:
# see the code in Chapter 6\parametric.jl
mutable struct Point{T}
x::T
y::T
end
This abstract type creates a whole family of new possible concrete types (but they are only compiled as needed at runtime), such as Point{Int64}, Point{Float64}, and Point{String}.
These are all subtypes of Point: Point{String} <: Point returns true. However, this is not the case when comparing different Point types, whose parameter types are subtypes of one another: Point{Float64} <: Point{Real} returns false.
To construct objects, you can indicate the type T in the constructor, as in p = Point{Int64}(2, 5), but this can be shortened to p = Point(2, 5). Or let's consider another example: p = Point("London", "Great-Britain").
If you want to restrict the parameter type T to only the subtypes of Real, this can be written as follows:
mutable struct Point{T <: Real}
x::T
y::T
end
Now, the statement p = Point("London", "Great-Britain") results in an ERROR: MethodError: `Point{T<:Real}` has no method matching Point{T<:Real}(::String, :String) error message, because String is not a subtype of Real.
In much the same way, methods can also optionally have type parameters immediately after their name and before the tuple of arguments. For example, to constrain two arguments to be of the same type T, run the following command:
add(x::T, y::T) where T = x + y
Now, add(2, 3) returns 5 and add(2, 3.0) returns an ERROR: MethodError: `add` has no method matching add(::Int64, ::Float64) error message.
Here, we restrict T to be a subtype of Number in add as follows:
add(x::T, y::T) where T <: Number = x + y
As another example, here is how to check whether a vecfloat function only takes a vector of floating point numbers as the input. Simply define it with a type parameter T as follows:
function vecfloat(x::Vector{T}) where T <: AbstractFloat
# code
end
Inner constructors can also take type parameters in their definition.
- 跟“龍哥”學(xué)C語(yǔ)言編程
- 精通搜索分析
- Practical DevOps
- Python數(shù)據(jù)挖掘與機(jī)器學(xué)習(xí)實(shí)戰(zhàn)
- PhoneGap:Beginner's Guide(Third Edition)
- 案例式C語(yǔ)言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)
- Python:Deeper Insights into Machine Learning
- Node.js區(qū)塊鏈開(kāi)發(fā)
- 從零學(xué)Java設(shè)計(jì)模式
- Keil Cx51 V7.0單片機(jī)高級(jí)語(yǔ)言編程與μVision2應(yīng)用實(shí)踐
- Java 11 and 12:New Features
- Raspberry Pi Robotic Projects
- MonoTouch應(yīng)用開(kāi)發(fā)實(shí)踐指南:使用C#和.NET開(kāi)發(fā)iOS應(yīng)用
- 軟件自動(dòng)化測(cè)試實(shí)戰(zhàn)解析:基于Python3編程語(yǔ)言
- 編程真好玩:從零開(kāi)始學(xué)網(wǎng)頁(yè)設(shè)計(jì)及3D編程