Structs in Julia
Immutable by default; mutable is a separate promise.
# Structs are immutable by default; mutable opts in per type.
struct Target
name::String
magnitude::Float64
end
mutable struct DomeState
open::Bool
azimuth::Int
end
vega = Target("Vega", 0.03)
println("$(vega.name) at magnitude $(vega.magnitude)")
dome = DomeState(false, 0)
dome.open = true
dome.azimuth = 245
println("Dome open at $(dome.azimuth) degrees")
How it works
struct Targetfields are fixed once constructed.mutable struct DomeStateopts into assignment.- Construction is positional:
Target("Vega", 0.03).
Keywords and builtins used here
BoolDomeStateFloat64IntStringTargetendfalsemutablestructtrue
The run, in numbers
- Lines
- 18
- Characters to type
- 361
- Tokens
- 79
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 68 seconds.
Step 1 of 3 in Types, step 16 of 27 in Language basics.