Stream: helpdesk (published)

Topic: Accessors.jl multi-set?


view this post on Zulip Alec (Aug 19 2025 at 22:01):

I might have missed it, but is there a way to set multiple fields at once? Something like:

obj = (a=1, b=(2, 3, "4"))

x = @set obj begin
_.a = 2
_b[2] = 4
end

Where the result is that x == (a=2, b=(2, 4, "4"))

@aplavin ?

view this post on Zulip Sundar R (Aug 19 2025 at 23:47):

Using setall with AccessorsExtra.jl's multi-optics:

julia> obj = (a=1, b=(2, 3, "4"))
(a = 1, b = (2, 3, "4"))

julia> using AccessorsExtra

julia> x = setall(obj, @optics(_.a, _.b[2]), [2, 4])
(a = 2, b = (2, 4, "4"))

view this post on Zulip Daniel Matz (Aug 19 2025 at 23:50):

Or you can use Chain.jl:

x = @chain obj begin
    @set _.a = 2
    @set _.b[2] = 4
end

view this post on Zulip aplavin (Aug 19 2025 at 23:52):

@Alec: are you just looking for more convenient syntax? For now, there's nothing special in Accessors, see https://github.com/JuliaObjects/Accessors.jl/issues/15.

view this post on Zulip aplavin (Aug 19 2025 at 23:53):

The recommended way is to use a piping package, such as DataPipes.jl highlighted in that issue:

using Accessors, DataPipes

obj = (a=1, b=(2, 3, "4"))

x = @p let
    obj
    @set __.a = 2
    @set __.b[2] = 4
end

view this post on Zulip aplavin (Aug 19 2025 at 23:56):

It's nontrivial to significantly improve over generic piping syntax like that :)

Recently (https://github.com/JuliaObjects/Accessors.jl/issues/204), there was another idea for multi-set syntax: @set obj.(a = 2, b[2] = 4). Maybe something along these lines would be a nice improvement?..


Last updated: Sep 07 2025 at 04:39 UTC