Assigning to a getter function

You are not really assigning to a “getter” function. You are assigning to a vector returned by a getter function. Your definition of position! is equivalent to the one below and it is perfectly valid.

function position!(p::AbstractPoint, new_pos)
    _position = position(p) # get the vector
    _position .= new_pos # set the elements of the vector via broadcast
    return nothing
end

As a side note, you may find this immutable version is more performant than the mutable version.

julia> struct StaticPoint{T,N} <: AbstractPoint{T}
           position::NTuple{N,T}
       end

julia> StaticPoint((3,5))
StaticPoint{Int64, 2}((3, 5))
1 Like