Skip to content

Instantly share code, notes, and snippets.

@apua
Created May 19, 2019 11:28
Show Gist options
  • Save apua/94f2223504f279399ce69620f33f64e4 to your computer and use it in GitHub Desktop.
Save apua/94f2223504f279399ce69620f33f64e4 to your computer and use it in GitHub Desktop.
Julia Type System Note

Reference: https://docs.julialang.org/en/v1/manual/types/index.html

Relationship between super/sub types:

Any >: Number >: Real >: Integer >: Signed >: Int === Int64

    >: Function >: getfield(Main, Symbol(”##1##2”))

                >: typeof(f)

    >: AbstractString >: String

    >: AbstractArray >: DenseArray >: Array

    >: AbstractArray{Any,1} >: DenseArray{Any,1} >: Array{Any, 1}

    >: Ref === Ref{} >: Ptr === Ptr{} >: Ptr{Cvoid}

                     >: Ref{Cvoid} >: Ptr{Cvoid}

    >: Expr

    >: Symbol

    >: Tuple >: Tuple{}

             >: Tuple{Any} >: Tuple{Numbr} >: ...  # `Tuple` is covariant

             >: Tuple{Any, Any}

             >: Tuple{Any, Any, Any}

             >: ...

    >: Vararg === Vararg{} >: Vararg{Int} >: Vararg{Int, 3}

    >: NamedTuple === NamedTuple{} >: NamedTuple{()} >: NamedTuple{(), Tuple{}}

    >: Type === Type{}
            === (Type{T} where T)
            == Type{<:Any}          >: Type{T}
                                    === supertype(DataType)
                                    === supertype(UnionAll) >: DataType

                                                            >: UnionAll

                                                            >: Union  # empty union

    >: Union{A, B} where A where B >: Union

                                   >: Union{Int, String} >: Union{Int, Int} === Int

    >: Union{} === Base.Bottom  # of type `Core.TypeofBottom`

UnionAll expresses the "iterated union" of types, e.g. A{B,C} is equivalent to A{B}{C}, is type of:

Type

Vararg
Vararg{Any}

NamedTuple
NamedTuple{()}

Array
Array{Any}
DenseArray
DenseArray{Any}
AbstractArray
AbstractArray{Any}

Ptr
Ref

Union{A, B} where A where B

C  # struct C{T} end
C{<:Any}
C{T} where T<:Any

DataType has properties: "explicitly declared", "named", "explicitly declared supertypes", "(optional) parameterized" is type of:

Int
Integer
Real
Number
Any

String
AbstractString

Tuple
Tuple{}

Vararg{Any, 0}

NamedTuple{(), Tuple{}}

Array{Any, 1}
DenseArray{Any, 1}
AbstractArray{Any, 1}

Ptr{Cvoid}
Ref{Cvoid}

Expr
Symbol

getfield(Main, Symbol("##1##2")  # function () end
typeof(f)  # function f() end
Function

DataType
UnionAll
Union
Type{T}  # supertype(DataType) ... it is not UnionAll !!

Union{Any}  # Any
Union{A} where A  # Any

C  # struct C end
C{Any}  # struct C{T} end

There are some types neither DataType nor UnionAll but still a Type:

julia> Union{Int, String} isa DataType
false

julia> Union{Int, String} isa Type
true

julia> Union{Int, String} isa Union
true

julia>

julia> Union{} isa DataType
false

julia> Union{} isa Type
true

julia> Union{} isa Union
false

julia> Union{} isa Core.TypeofBottom
true
@apua
Copy link
Author

apua commented May 19, 2019

  typeof supertype
1 Int64 x
Int64 DataType Signed
DataType DataType Type{T}
Type{T} DataType Any
Any DataType Any
Signed DataType Integer
Integer DataType Real
     
Type UnionAll Any
UnionAll DataType Type{T}
     
“” String x
String DataType AbstractString
AbstractString DataType Any
     
() Tuple{} x
Tuple{} DataType Any
     
t = (a=0,) NamedTuple{(:a,),Tuple{Int64}} x
NamedTuple{(:a,)} UnionAll Any
NamedTuple{(:a,),Tuple{Int64}} DataType Any
     
[] Array{Any, 1} x
Array{Any, 1} DataType DenseArray{Any, 1}
DenseArray{Any, 1} DataType AbstractArray{Any,1}
AbstractArray{Any,1} DataType Any
Array UnionAll DenseArray
DenseArray UnionAll AbstractArray
AbstractArray UnionAll Any
     
Ptr UnionAll Ref
Ref UnionAll Any
Ptr{Apua} DataType Ref{Apua}
Ref{Apua} DataType Any
     
:() Expr x
Expr DataType Any
     
:a Symbol x
Symbol DataType Any
     
function f() end typeof(f) x
typeof(f) DataType Function
Function DataType Any
     
g = function()end getfield(Main, Symbol(”##1##2”)) x
getfield(Main, Symbol(”##1##2”)) DataType Function
     
u = Union{A, B} where A where B UnionAll x
     
Union{Int, String} Union x
Union DataType Type{T}
     
Vararg{} UnionAll Any
Vararg{Int} UnionAll Any
Vararg{Int, 3} DataType Any
     
Pointy UnionAll Any
Pointy{>:Int} UnionAll Any
Pointy{Int} DataType Any

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment