# Assigning to a getter function

**URL:** <https://discourse.julialang.org/t/assigning-to-a-getter-function/86278>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 24, 2022, 5:28pm UTC](https://discourse.julialang.org/t/assigning-to-a-getter-function/86278 "2022-08-24T17:28:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![StackEnjoyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stackenjoyer/32/39069_2.png) [@StackEnjoyer](https://discourse.julialang.org/u/StackEnjoyer)\
**Post date:** [August 24, 2022, 5:28pm UTC](https://discourse.julialang.org/t/assigning-to-a-getter-function/86278/1 "2022-08-24T17:28:51Z")

</div>

# Getter and setter functions

Recently I stumbled on some interesting behaviour when using a “getter”-like function in combination with broadcasting calls. For the sake of argument lets assume:

```julia-auto
abstract type AbstractPoint{T} end

mutable struct Point{T} <: AbstractPoint{T}
    position::Vector{T}
end

```

where the `position` method returns the `position` field, assuming that the interface for `AbstractPoint` requires that every inheriting type has such a field:

```julia-auto
position(p::AbstractPoint) = p.position

```

As far as I have observed it, a Julia-esque way to update the `position` of an instance of `Point` and other `AbstractPoint`s would be:

```julia-auto
position!(p::AbstractPoint, new_pos) = (p.position = new_pos)

```

## Assigning to a getter function?

However, what also appears to work, is to write:

```julia-auto
function position!(p::AbstractPoint, new_pos)
    position(p) .= new_pos
    return nothing
end

```

The above function only works when broadcasting `.=` and, naturally, also for non-mutable versions of `Point`. I am not certain why this works, but it enables the reuse of `position!` by other subtypes of `AbstractPoint` when changing the getter method for `position(...)`, i.e., if those subtypes do not have a field `position` but rather a composite type that does have a `position` field.

This syntax does not work with non-Array types.

## The question

Has anyone here ever used the above syntax for a setter function intentionally? In which ways can this cause unwanted effects? I’m still to new to Julia to be familiar enough with the broadcasting system to fully understand the workings of this code. The use cases for this are rather limited, since it only works for Arrays, but my benchmarking attempt has shown no allocations for either version of `position!`. Feedback is highly welcome 🙂

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 24, 2022, 5:51pm UTC](https://discourse.julialang.org/t/assigning-to-a-getter-function/86278/2 "2022-08-24T17:51:28Z")

</div>

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.

```julia
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
julia> struct StaticPoint{T,N} <: AbstractPoint{T}
           position::NTuple{N,T}
       end

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

```

---

<div class="post-metadata">

**Author:** ![StackEnjoyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stackenjoyer/32/39069_2.png) [@StackEnjoyer](https://discourse.julialang.org/u/StackEnjoyer)\
**Post date:** [August 24, 2022, 6:02pm UTC](https://discourse.julialang.org/t/assigning-to-a-getter-function/86278/3 "2022-08-24T18:02:16Z")

</div>

Thank you for the response. Regarding `StaticPoint`, would I still be able to modify the `position` field? This only works in the above example due to the broadcasting call.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 24, 2022, 6:09pm UTC](https://discourse.julialang.org/t/assigning-to-a-getter-function/86278/4 "2022-08-24T18:09:25Z")

</div>

> [@StackEnjoyer](#):
>
> Regarding `StaticPoint`, would I still be able to modify the `position` field?

In general, no. The idea is that if you need to modify the `position` field, then it might be better just to create a new `StaticPoint`.

As for why this might be better, see the discussion here:

> <https://stackoverflow.com/questions/59737850/why-are-immutable-structs-default-in-julia>
