11  Types

Julia’s type system is what defines its usage and guides its high performance.

User-defined types are created using struct.

Defining variable types is optional, but helps the compiler produce optimized (i.e. very fast) code.

In the following example we start by loading the Dates package to use the Date type within the struct definition, along with the base String and Float64 types:

using Dates
struct Person
    FirstName::String
    LastName::String
    DOB::Date
    Height::Float64
    Weight::Float64
end
typeof(Person)
DataType
p = Person("Julia", "Lang", Date(2012, 2, 12), 1.73, 51)
Person("Julia", "Lang", Date("2012-02-12"), 1.73, 51.0)

11.1 Methods

Get all methods that can be applied to a DataType with methodswith():

methodswith(Date)
43-element Vector{Method}:

Get all data types that have methods defined for a generic with methods():

methods(sort)
# 4 methods for generic function sort from Base:
  • sort(r::AbstractUnitRange) in Base at range.jl:1410
  • sort(r::AbstractRange) in Base at range.jl:1413
  • sort(v::AbstractVector; kws...) in Base.Sort at sort.jl:1489
  • sort(A::AbstractArray{T}; dims, alg, lt, by, rev, order, scratch) where T in Base.Sort at sort.jl:1783