Mutating a mutable field of an immutable and exclamation mark

question
#1

Should one decorate the name of the function that modifies a mutable field of an immutable structure with an exclamation mark? For instance, should one call the function in the example below “optimize!” or “optimize”?

struct SomeData
    x::Int
end

struct Augmentation
    y::Vector{Int}
end

Augmentation()=Augmentation([0])

struct AugmentedData
    s::SomeData
    a::Augmentation
end

AugmentedData(s::SomeData)=AugmentedData(s, Augmentation())

function optimize!(ad::AugmentedData)
    ad.a.y[1]=42
    nothing
end

julia> ad=AugmentedData(s)
AugmentedData(SomeData(1), Augmentation([0]))
julia> optimize!(ad)
julia> ad
AugmentedData(SomeData(1), Augmentation([42]))
0 Likes

#2

optimize!, because you are mutating the contents.

2 Likes

#3

This is what I think, too. But it looks a bit weird for a function taking a single immutable argument.

0 Likes