https://github.com/juliaobjects/accessors.jl
Update immutable data
https://github.com/juliaobjects/accessors.jl
immutable julia lens
Last synced: 18 days ago
JSON representation
Update immutable data
- Host: GitHub
- URL: https://github.com/juliaobjects/accessors.jl
- Owner: JuliaObjects
- License: other
- Created: 2020-09-08T14:53:47.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-02-25T07:13:19.000Z (2 months ago)
- Last Synced: 2025-04-07T20:06:20.199Z (26 days ago)
- Topics: immutable, julia, lens
- Language: Julia
- Homepage:
- Size: 2.22 MB
- Stars: 194
- Watchers: 8
- Forks: 20
- Open Issues: 27
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Accessors
[](https://juliaobjects.github.io/Accessors.jl/stable/)
[](https://juliaobjects.github.io/Accessors.jl/dev/)
The goal of [Accessors.jl](https://github.com/JuliaObjects/Accessors.jl) is to make updating immutable data simple.
It is the successor of [Setfield.jl](https://github.com/jw3126/Setfield.jl).# Usage
Say you have some immutable data structure, such as a `NamedTuple`:
```julia
julia> nt = (a=1, b=2)
(a = 1, b = 2)```
If you try something like `nt.b=3`, it will throw an error. But
using Accessors, we can change it anyways:
```julia
julia> using Accessorsjulia> @set nt.b=3
(a = 1, b = 3)
```Note that this only returns an updated copy of `nt`, and *does not overwrite or mutate* the
value bound to `nt`:
```julia
julia> nt
(a = 1, b = 2)
```To overwrite the old definition, we can rebind `nt` to the new version:
```julia
julia> nt = @set nt.b=3
(a = 1, b = 3)julia> nt
(a = 1, b = 3)
```As this is a common use case, the convenience macro `@reset` rebinds the variable (`nt`) to the updated version:
```julia
julia> @reset nt.b=4
(a = 1, b = 4)julia> nt
(a = 1, b = 4)
```For more detail, see [this tutorial](https://juliaobjects.github.io/Accessors.jl/stable/getting_started/) and/or watch this video:
[](https://youtu.be/vkAOYeTpLg0 "Changing the immutable")
# Featured extensions
- [AccessorsExtra.jl](https://github.com/JuliaAPlavin/AccessorsExtra.jl) [[docs](https://aplavin.github.io/AccessorsExtra.jl/test/notebook.html)] introduces additional optics and related functions that are considered too opinionated or too experimental for inclusion in `Accessors`.