2  Type

2.1 Check type by isa

isa(1, Int)
#> true
isa(1, Int64)
#> true
isa(1, Int128)
#> false

or

1 isa Int
#> true

2.2 User-defined Type

let’s create a struct to represent scientific open source programming languages.

struct Language
    name::String
    title::String
    year_of_birth::Int64
    fast::Bool
end

Access field names

fieldnames(Language)
#> (:name, :title, :year_of_birth, :fast)

2.2.1 Initiate objects (immutable)

julia = Language("Julia", "Rapidus", 2012, true)
#> Language("Julia", "Rapidus", 2012, true)
R = Language("R", "R & R", 1993, false)
#> Language("R", "R & R", 1993, false)
typeof(julia)
#> Language

2.2.2 Accessing individual values

julia.name
#> "Julia"
R.name
#> "R"

2.2.3 Printing with Base.show method

"Hello World"
#> "Hello World"
Base.show("Hello World")
#> "Hello World"

Implement using print()

print("Hello World")
#> Hello World
using Dates
function Base.show(io::IO, l::Language)

    years_old = year(today()) - l.year_of_birth
    print(io, "$(l.name) is $years_old years old,")
    print(io, " created by $(l.title).") 

    return nothing
end
julia
#> Julia is 10 years old, created by Rapidus.
R
#> R is 29 years old, created by R & R.