typestar

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

  1. struct Target fields are fixed once constructed.
  2. mutable struct DomeState opts into assignment.
  3. Construction is positional: Target("Vega", 0.03).

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Types, step 16 of 27 in Language basics.

← Previous Next →

Structs in other languages