using InteractiveUtils
include(joinpath(@__DIR__, "observations.jl"))
struct Station
x::Int
y::Float64
label
end
s=Station(7.0,11,:bay)
probe("struct_construct",(7.0,11,:bay),(typeof(s),s.x,s.y,s.label,fieldtypes(Station),
outcome(() -> Station(7.5,11,:bay)),typeof(Station(7,11,"bay").label)),
(Station,7,11.0,:bay,(Int,Float64,Any),"InexactError",String))
probe("struct_immutable",s,(isimmutable(s),outcome(() -> setproperty!(s,:x,9))), (true,"ErrorException"))
struct Position
x::Int
y::Int
end
struct Payload
samples::Vector{Int}
end
shared=[7]
a,b=Payload(shared),Payload(shared)
c=Payload([7])
push!(a.samples,11)
probe("struct_immutable_identity","two scalar records; same and distinct inner vectors",
(Position(7,11)===Position(7,11),a===b,a===c,b.samples,c.samples),
(true,true,false,[7,11],[7]))
mutable struct StationStack
values::Vector{Any}
label::String
end
q=StationStack(Any[],"incoming")
push!(q.values,7)
q.label="reviewed"
probe("struct_mutable",("incoming",7,"reviewed"),(q.values,q.label,isimmutable(q)),(Any[7],"reviewed",false))
left=StationStack(Any[7],"north")
right=StationStack(Any[7],"north")
default_equal=(left===right,left==right)
Base.:(==)(a::StationStack,b::StationStack)=a.label==b.label && a.values==b.values
probe("struct_value_equality","equal content in two mutable instances",(default_equal,left==right,left===right),
((false,false),true,false))
mutable struct Sensor
reading::Int
const calibration::Float64
end
sensor=Sensor(7,1.5)
sensor.reading=11
probe("struct_const", (7,1.5,11),(sensor.reading,sensor.calibration,
outcome(() -> setproperty!(sensor,:calibration,3.0))),(11,1.5,"ErrorException"))
abstract type Reading end
abstract type NumericReading <: Reading end
struct CountReading <: NumericReading
value::Int
end
probe("struct_abstract",Reading,(NumericReading<:Reading,isabstracttype(Reading),
outcome(() -> Reading()),CountReading(7) isa Reading),
(true,true,"MethodError",true))
probe("struct_number_tree","selected numeric families",
(Complex{Int}<:Number,Integer<:Real,Int<:AbstractFloat,Float64<:AbstractFloat,BigFloat<:AbstractFloat,
Bool<:Integer,BigInt<:Signed,UInt16<:Unsigned,Rational{Int}<:Real,typeof(pi)<:Real,Any<:Any),
(true,true,false,true,true,true,true,true,true,true,true))
struct PairOf{T}
first::T
last::T
end
probe("struct_parameter",(7,11,7.0,11.0),(typeof(PairOf(7,11)),typeof(PairOf(7.0,11.0)),
outcome(() -> PairOf(7,11.0)),PairOf{Float64}(7,11).first),
(PairOf{Int},PairOf{Float64},"MethodError",7.0))
struct RealPair{T<:Real}
first::T
last::T
end
probe("struct_bounded_parameter",(7,"north"),(typeof(RealPair(7,11)),
outcome(() -> RealPair(7,"north")),outcome(() -> RealPair{String}("a","b"))),
(RealPair{Int},"MethodError","TypeError"))
struct SampleBatch{T}
rows::Vector{T}
end
struct OptionalCount
value::Union{Int,Nothing}
end
probe("struct_container_union",([7,11],nothing),
(typeof(SampleBatch([7,11])),OptionalCount(7).value,OptionalCount(nothing).value),
(SampleBatch{Int},7,nothing))
probe("struct_invariance","Vector{Int} and abstract element types",
(Int<:Real,Vector{Int}<:Vector{Real},Vector{Int}<:Vector{<:Real}),(true,false,true))
struct Twin
a
b
end
Twin(x)=Twin(x,x)
struct BoundsPair
lower::Real
upper::Real
BoundsPair(lo,hi)=lo<=hi ? new(lo,hi) : throw(ArgumentError("upper bound below lower"))
end
pair=BoundsPair(7,11)
probe("struct_constructors",(7,11),(Twin(7).a,Twin(7).b,pair.lower,pair.upper,outcome(() -> BoundsPair(11,7))),
(7,7,7,11,"ArgumentError"))
@enum Signal Idle Running Paused
@enum Source North=4 South West
probe("struct_enum","default and explicit starting values",
(Int(Idle),Int(Paused),Int(North),Source(5),Int(West),outcome(() -> Source(100))),
(0,2,4,South,6,"ArgumentError"))
probe("struct_typeof",s,typeof(s),Station)
probe("struct_isa",7,(isa(7,Int),isa(7,Any),isa(7,Float64)),(true,true,false))
probe("struct_subtype",Int,(Int<:Integer,Int<:Real,Int<:AbstractFloat),(true,true,false))
probe("struct_supertype",Int,supertype(Int),Signed)
probe("struct_subtypes",Reading,sort!(string.(subtypes(Reading))),["NumericReading"])
probe("struct_fieldnames",Station,fieldnames(Station),(:x,:y,:label))
probe("struct_fieldtypes",Station,fieldtypes(Station),(Int,Float64,Any))
probe("struct_union",Union{Int,Nothing},(7 isa Union{Int,Nothing},nothing isa Union{Int,Nothing},
"north" isa Union{Int,Nothing}),(true,true,false))
probe("struct_bottom",Union{},(Union{}<:Int,Union{}<:Any,7 isa Union{}),(true,true,false))
probe("struct_nothing",nothing,(typeof(nothing),nothing===nothing,nothing===missing),(Nothing,true,false))
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录
入口文件:projects/julia-types-methods/types.jl
本地运行步骤快速上手
- 点击上方“保存源码”保存为
types.jl,或下载右侧课程工程包。 - 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip
课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202612a215.zip