Types and type declarations
Contents
Types and type declarations¶
x = 100.0
typeof(x)
Float64
function foo()
x::Int8 = 100
x
end
x = foo()
typeof(x)
Int8
Type trees¶
print_tree(AbstractArray)
UndefVarError: print_tree not defined
Stacktrace:
[1] top-level scope
@ In[5]:1
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
Types in function arguments¶
function g(x, y)
return x * y
end
x = g(2, 3)
@show x
typeof(x)
x = 6
Int64
x = g(2.0, 3.0)
@show x
typeof(x)
x = 6.0
Float64
x = g("foo", "bar")
@show x
typeof(x)
x = "foobar"
String
function f(x::Number, y::Number)
return x * y
end
x = f(2, 3)
@show x
typeof(x)
x = 6
Int64
x = f("foo", "bar")
MethodError: no method matching f(::String, ::String)
Stacktrace:
[1] top-level scope
@ In[10]:1
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
Type Unions¶
function h(x::Union{Integer, String}, y::Union{Integer, String})
x * y
end
@show h(1, 2)
@show h("foo", "bar");
h(1, 2) = 2
h("foo", "bar") = "foobar"
h(1.0, 2.0)
MethodError: no method matching h(::Float64, ::Float64)
Stacktrace:
[1] top-level scope
@ In[12]:1
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
Composite Types¶
struct Point2D
x::Float64
y::Float64
end
p = Point2D(1.0, 2.0)
@show p.x
@show p.y;
p.x = 1.0
p.y = 2.0
Parametric Composite Types¶
struct Point3D{T}
x::T
y::T
z::T
end
p = Point3D{Int8}(1, 2, 3)
@show p.x
typeof(p.y)
p.x = 1
Int8
p = Point3D{Float32}(1.0, 2.0, 3.0)
typeof(p.x)
Float32
p = Point3D{String}("foo", "bar", "foobar")
typeof(p.x)
String
Type restrictions on parameters¶
struct NumericPoint2D{T<:Real}
x::T
y::T
end
p = NumericPoint2D{Float64}(1.0, 2.0)
@show typeof(p.x)
q = NumericPoint2D{Int8}(1, 2)
@show typeof(q.x)
typeof(p.x) = Float64
typeof(q.x) = Int8
Int8
NumericPoint2D{String}("foo", "bar")
TypeError: in NumericPoint2D, in T, expected T<:Real, got Type{String}
Stacktrace:
[1] top-level scope
@ In[18]:1
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
import Base.+
Functions and operators with parametric types¶
+(A::NumericPoint2D{T}, B::NumericPoint2D{T}) where T = NumericPoint2D{T}(A.x + B.x, A.y + B.y)
+ (generic function with 209 methods)
A = NumericPoint2D{Int32}(1, 2)
B = NumericPoint2D{Int32}(1, 2)
A + B
NumericPoint2D{Int32}(2, 4)
Fields of composite types are immutable by default
A.x = 2
setfield!: immutable struct of type NumericPoint2D cannot be changed
Stacktrace:
[1] setproperty!(x::NumericPoint2D{Int32}, f::Symbol, v::Int64)
@ Base ./Base.jl:43
[2] top-level scope
@ In[22]:1
[3] eval
@ ./boot.jl:373 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
Mutable composite types¶
mutable struct MutablePoint2D{T<:Real}
x::T
y::T
end
A = MutablePoint2D{Int32}(1, 2)
A.x = 2
@show A.x;
A.x = 2
Constructors¶
struct Polar{T<:Real}
r::T
θ::T
end
A = Polar(1.0, π / 4)
A.θ
0.7853981633974483
function Polar(A::NumericPoint2D{T}) where T
r = sqrt(A.x ^ 2 + A.y ^ 2)
θ = atan(A.y, A.x)
Polar{T}(r, θ)
end
Polar
A = NumericPoint2D{Float32}(cos(π / 4), sin(π / 4))
B = Polar(A)
@show B.r
@show B.θ
typeof(B.r)
B.r = 0.99999994f0
B.θ = 0.7853982f0
Float32