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 ?
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"))
Or you can use Chain.jl:
x = @chain obj begin
@set _.a = 2
@set _.b[2] = 4
end
@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.
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
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