Julia 的数据类型

             

  • 本词条处于草稿阶段.

变量类型

   参考 Julia 文档

   常见类型 Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Float16, Float32, Float64, ComplexF16, ComplexF32ComplexF64

   BigInt 是任意大小的整数,BigFloat 是任意精度浮点数.

julia> BigFloat(2.1) # 2.1 here is a Float64
2.100000000000000088817...

julia> BigFloat("2.1") # the closest BigFloat to 2.1
2.099999999999999999999999999...99999999999999999999986

julia> BigFloat("2.1", RoundUp)
2.100000000000000000000000000...00000000000000000000021

julia> BigFloat("2.1", RoundUp, precision=128)
2.100000000...00000000000007

   使用 typeof(var) 来询问变量类型,用 isa(var, type) 来确定类型.

抽象类型

   声明抽象类型

abstract type 子类名 <: 母类名 end
例子
abstract type Number end
abstract type Real     <: Number end
abstract type AbstractFloat <: Real end
abstract type Integer  <: Real end
abstract type Signed   <: Integer end
abstract type Unsigned <: Integer end
最高级的抽象类是 Any,这里的 Number 就是 Any 的子类.

Fig
Fig. 1:Julia 自带类型结构

   以及

Complex{T<:Real} <: Number
Rational{T<:Integer} <: Real
Irrational{sym} <: AbstractIrrational

原始类型

   原始类型(primitive type)是有 bit 组成的具体类型.Julia 可以自定义原始类型.定义原始类型的格式为

primitive type 类名 比特数 end
primitive type 类名 <: 父类名 比特数 end
例如
primitive type Float16 <: AbstractFloat 16 end
primitive type Float32 <: AbstractFloat 32 end
primitive type Float64 <: AbstractFloat 64 end

primitive type Bool <: Integer 8 end
primitive type Char <: AbstractChar 32 end

primitive type Int8    <: Signed   8 end
primitive type UInt8   <: Unsigned 8 end
primitive type Int16   <: Signed   16 end
primitive type UInt16  <: Unsigned 16 end
primitive type Int32   <: Signed   32 end
primitive type UInt32  <: Unsigned 32 end
primitive type Int64   <: Signed   64 end
primitive type UInt64  <: Unsigned 64 end
primitive type Int128  <: Signed   128 end
primitive type UInt128 <: Unsigned 128 end

复合类型

   复合类型(composite) 类似于 C++ 中的 struct

struct Foo
           bar # 抽象类型是 Any
           baz::Int
           qux::Float64
       end
创建该类型的对象
foo = Foo("Hello, world.", 23, 1.5) # 叫做 constructor
查看成员名称用 fieldnames(Foo)

   要获取成员的值,用 foo.bar, foo.baz, foo.qux 等.用 struct 声明的复合类型值在生成后就不能被改变.如果 struct 里面有矩阵等,那么矩阵元仍然是 mutable 的(没有 lower level const).

   如果要支持成员改变,那么用 mutable struct 来声明即可.mutable struct 通常存在 heap 中而不是在 stack 中.

Type Unions

IntOrString = Union{Int,AbstractString}
1 :: IntOrString
"Hello!" :: IntOrString
1.0 :: IntOrString # 异常
但是,这和使用 <: 有什么区别?一个类只可能有一个非 Union 的母类,如果一个函数的参数想要支持两个母类不同的类怎么办?那就用 Union 即可.

   另外一个应用的例子是 Union{T, Nothing} 作为函数参数,这样这个参数就可以忽略了.

含参类型

   复数类型 ComplexF16, ComplexF32ComplexF64Complex{Float16}, Complex{Float32}Complex{Float64} 的别名.Complex 就是一个含参类型(parametric type)

   例如

julia> struct Point{T}
           x::T
           y::T
       end
那么 Point{Float64} 等都是合法的具体类型.

   Point 本身是一个抽象类,是其具体类的母类

julia> Point{Float64} <: Point
true
julia> Point{AbstractString} <: Point
true
<: 相当于一个二元算符,输出 Bool

   例子:

function norm(p::Point{Real})
    sqrt(p.x^2 + p.y^2)
end
constructor
p = Point{Float64}(1.0, 2.0)

含参抽象类型

   含参抽象类型(Parametric Abstract Types) 例如

abstract type Pointy{T} end
struct Point{T} <: Pointy{T}
           x::T
           y::T
       end

Tuple Types

   Tuple 是函数参数列表的抽象.例如两个元素的 tuple type 类似于如下的含参 struct 类型

struct Tuple2{A,B}
    a::A
    b::B
end
例子
julia> typeof((1,"foo",2.5))
Tuple{Int64, String, Float64}
julia> Tuple{Int,AbstractString} <: Tuple{Real,Any}
true

julia> Tuple{Int,AbstractString} <: Tuple{Real,Real}
false

Vararg Tuple Types

Named Tuple Types

Parametric Primitive Types

UnionAll Types

Singleton types

Type{T} type selectors

Type Aliases

   使用等号.例如

# 32-bit system:
julia> UInt
UInt32

# 64-bit system:
julia> UInt
UInt64
实现方法
This is accomplished via the following code in base/boot.jl:

if Int === Int64
    const UInt = UInt64
else
    const UInt = UInt32
end

Operations on Types

julia> isa(1, Int)
true

julia> typeof(Rational{Int})
DataType

julia> typeof(Union{Real,String})
Union

julia> typeof(DataType)
DataType

julia> typeof(Union)
DataType

julia> supertype(Float64)
AbstractFloat

julia> supertype(Number)
Any

julia> supertype(AbstractString)
Any

julia> supertype(Any)
Any

         

© 小时科技 保留一切权利