isa(1, Int)
#> true
isa(1, Int64)
#> true
isa(1, Int128)
#> false
2 Type
2.1 Check type by isa
or
1 isa Int
#> true
2.2 User-defined Type
let’s create a struct
to represent scientific open source programming languages.
struct Language
::String
name::String
title::Int64
year_of_birth::Bool
fastend
Access field names
fieldnames(Language)
#> (:name, :title, :year_of_birth, :fast)
2.2.1 Initiate objects (immutable)
= Language("Julia", "Rapidus", 2012, true)
julia #> Language("Julia", "Rapidus", 2012, true)
= Language("R", "R & R", 1993, false)
R #> 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)
= year(today()) - l.year_of_birth
years_old 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.