类型系统概述 #
一、类型系统基础 #
1.1 动态类型 #
Julia是动态类型语言,变量的类型在运行时确定:
julia
x = 10
typeof(x)
x = "hello"
typeof(x)
1.2 类型注解 #
可以使用::添加类型注解:
julia
x::Int = 10
y::Float64 = 3.14
1.3 类型推断 #
Julia编译器会自动推断类型:
julia
function add(a, b)
a + b
end
@code_warntype add(1, 2)
二、类型层次结构 #
2.1 类型树 #
Julia类型形成一棵树,Any是根节点:
text
Any
├── Number
│ ├── Real
│ │ ├── Integer
│ │ │ ├── Int8, Int16, Int32, Int64, Int128
│ │ │ ├── UInt8, UInt16, UInt32, UInt64, UInt128
│ │ │ └── Bool
│ │ ├── AbstractFloat
│ │ │ ├── Float16, Float32, Float64
│ │ │ └── BigFloat
│ │ └── Rational
│ └── Complex
├── AbstractString
│ ├── String
│ └── SubString
├── AbstractArray
│ ├── Array
│ └── ...
└── ...
2.2 查看类型层次 #
julia
supertype(Int)
supertype(Integer)
supertype(Real)
supertype(Number)
subtypes(Integer)
subtypes(Real)
subtypes(Number)
2.3 类型检查 #
julia
1 isa Int
1 isa Integer
1 isa Number
1 isa Real
Int <: Integer
Integer <: Real
Real <: Number
Float64 <: Integer
三、抽象类型 #
3.1 定义抽象类型 #
julia
abstract type Animal end
abstract type Mammal <: Animal end
abstract type Bird <: Animal end
3.2 抽象类型不能实例化 #
julia
Animal()
3.3 类型层次 #
julia
Mammal <: Animal
Bird <: Animal
Mammal <: Bird
四、具体类型 #
4.1 原始类型 #
julia
primitive type MyInt 64 end
4.2 复合类型 #
julia
struct Point
x::Float64
y::Float64
end
p = Point(1.0, 2.0)
typeof(p)
4.3 可变复合类型 #
julia
mutable struct MutablePoint
x::Float64
y::Float64
end
p = MutablePoint(1.0, 2.0)
p.x = 10.0
五、类型操作 #
5.1 typeof #
julia
typeof(1)
typeof(1.0)
typeof("hello")
typeof([1, 2, 3])
5.2 isa #
julia
1 isa Int
1.0 isa Int
1.0 isa Float64
5.3 <: (子类型) #
julia
Int <: Integer
Float64 <: Real
String <: AbstractString
5.4 >: (超类型) #
julia
Number >: Int
Integer >: Int
5.5 typejoin #
julia
typejoin(Int, Float64)
typejoin(Int, String)
六、Union类型 #
6.1 基本用法 #
julia
IntOrFloat = Union{Int, Float64}
1 isa IntOrFloat
1.0 isa IntOrFloat
"hello" isa IntOrFloat
6.2 函数参数 #
julia
function process(x::Union{Int, Float64})
x * 2
end
process(10)
process(10.5)
process("hello")
6.3 UnionAll #
julia
Vector
Vector{Int}
Vector{Float64}
Array
Array{Int, 1}
Array{Float64, 2}
七、类型推断 #
7.1 @code_warntype #
检查类型稳定性:
julia
function unstable(x)
if x > 0
return x
else
return "negative"
end
end
@code_warntype unstable(1)
7.2 类型稳定性 #
类型稳定的函数性能更好:
julia
function stable(x::Int)
if x > 0
return x
else
return -x
end
end
@code_warntype stable(1)
八、实践练习 #
8.1 练习1:类型层次 #
julia
function print_type_hierarchy(T, level=0)
println(" "^level * string(T))
for S in subtypes(T)
print_type_hierarchy(S, level + 1)
end
end
print_type_hierarchy(Number)
8.2 练习2:类型检查函数 #
julia
function type_info(x)
println("Value: $x")
println("Type: $(typeof(x))")
println("Supertype: $(supertype(typeof(x)))")
println("Is concrete: $(isconcretetype(typeof(x)))")
end
type_info(1)
type_info(1.0)
type_info("hello")
8.3 练习3:类型分发 #
julia
function describe(x)
if x isa Integer
"Integer: $x"
elseif x isa AbstractFloat
"Float: $x"
elseif x isa AbstractString
"String: $x"
else
"Unknown type: $(typeof(x))"
end
end
describe(1)
describe(1.0)
describe("hello")
九、总结 #
本章我们学习了:
- 类型系统基础:动态类型和类型注解
- 类型层次:Any为根的类型树
- 抽象类型:定义类型层次
- 具体类型:原始类型和复合类型
- 类型操作:typeof、isa、<:
- Union类型:组合类型
接下来让我们学习Julia的复合类型!
最后更新:2026-03-27