liveBook · Manning

Mutable object

As explained in Julia as a Second Language

A mutable type is a Julia type whose instances can have their fields modified after creation.

A mutable object can have its fields changed after creation. In Julia, a composite type defined with `struct` is immutable by default. Add `mutable` when operations need to update an instance’s fields, such as an archer spending arrows or a rocket changing its active stage. Immutability can help avoid bugs, so make mutability an explicit choice. section 7.3.2 section 7.5

Spend an arrow

```julia mutable struct Archer arrows::Int end function shoot!(archer::Archer) if archer.arrows > 0 archer.arrows -= 1 end archer end julia> robin = Archer(24) Archer(24) julia> shoot!(robin) Archer(23) ``` The `!` signals a mutating function by convention; it does not make mutation happen. Returning the changed object makes the result easy to inspect in the REPL. section 7.3.2

Mutability also supports collection-like operations. A staged rocket can update its `nextstage` field, while a space vehicle updates `activestage` as stages are added or removed with `pushfirst!` and `popfirst!`. These operations need mutable structs because they reassign fields. Changing a type definition itself is different from changing an instance: after making an existing struct mutable, restart the Julia REPL because types cannot be modified at runtime. section 12.6.1

Listing 12.28 Removing stages from the bottom
import Base: popfirst!, pushfirst!
 
tail(r::StagedRocket) = r.nextstage
tail(r::Rocket) = nothing
 
function popfirst!(ship::SpaceVehicle)
    r = tail(ship.activestage)
    if r == nothing                           #1
        throw(throw(ArgumentError
("no rocket stages left")))                   #2
    else
        discarded = ship.activestage          #3
        discarded.nextstage = emptypayload    #4
        ship.activestage = r                  #5
    end
    discarded                                 #6
 end

Follow `popfirst!` as it detaches the current stage, changes the vehicle’s active-stage field, and returns the discarded stage. section 12.6.1

How Julia as a Second Language covers it

How deep it goes
Defines mutability, demonstrates field updates, and explains why changing a type definition requires restarting the REPL.
Examples
Uses an archer spending arrows and a staged rocket adding or removing stages.
What it assumes
Assumes basic familiarity with Julia structs and functions while explaining mutability and the `mutable` keyword directly.
What you can do after
Build it from scratch: define a mutable struct and write functions that update its fields.
How it connects
Connects composite types and fields to mutating functions, methods, and collection operations.

Also explained in

Compare how 4 books explain mutable object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.