Skip to content

Modify array of immutables in place #134

@non-Jedi

Description

@non-Jedi

If I have an array of immutable datastructures, it would be extremely advantageous from a performance perspective to be able to mutate that array in place instead of replacing it. The @set and @set! macros don't currently support this.

julia> using Setfield

julia> x = [(a=rand(), b=rand()) for _=1:2]
2-element Array{NamedTuple{(:a, :b),Tuple{Float64,Float64}},1}:
 (a = 0.13871066644823227, b = 0.12386194648118187)
 (a = 0.46679798730440436, b = 0.6032333077678709)

julia> y = x;

julia> @set! x[1].a = 0.0
2-element Array{NamedTuple{(:a, :b),Tuple{Float64,Float64}},1}:
 (a = 0.0, b = 0.12386194648118187)
 (a = 0.46679798730440436, b = 0.6032333077678709)

julia> y
2-element Array{NamedTuple{(:a, :b),Tuple{Float64,Float64}},1}:
 (a = 0.13871066644823227, b = 0.12386194648118187)
 (a = 0.46679798730440436, b = 0.6032333077678709)

Any ideas on supporting this use-case?

EDIT: possible duplicate of #32

EDIT2: and possibly solved by the macro definitions in the examples folder? Is there a reason those are an example rather than an exported part of Setfield.jl's public api?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions