/
setfield.jl
71 lines (51 loc) · 1.68 KB
/
setfield.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module SetfieldImpl
export @set!!, prefermutation
using ..BangBang: setproperty!!, setindex!!
using Setfield: ComposedLens, DynamicIndexLens, IndexLens, Lens, PropertyLens, Setfield, set
struct Lens!!{T<:Lens} <: Lens
lens::T
end
Setfield.get(obj, lens::Lens!!) = get(obj, lens.lens)
# Default to immutable:
Setfield.set(obj, lens::Lens!!, value) = set(obj, lens.lens, value)
Setfield.set(obj, lens::Lens!!{<:ComposedLens}, value) =
set(obj, Lens!!(lens.lens.outer) ∘ Lens!!(lens.lens.inner), value)
Setfield.set(obj, ::Lens!!{<:PropertyLens{fieldname}}, value) where {fieldname} =
setproperty!!(obj, fieldname, value)
indicesfor(lens::IndexLens, _) = lens.indices
indicesfor(lens::DynamicIndexLens, obj) = lens.f(obj)
if isdefined(Setfield, :ConstIndexLens)
using Setfield: ConstIndexLens
const SupportedIndexLens = Union{ConstIndexLens,DynamicIndexLens,IndexLens}
indicesfor(::ConstIndexLens{I}, _) where {I} = I
else
const SupportedIndexLens = Union{DynamicIndexLens,IndexLens}
end
Setfield.set(obj, lens::Lens!!{<:SupportedIndexLens}, value) =
setindex!!(obj, value, indicesfor(lens.lens, obj)...)
"""
prefermutation(lens::Lens) :: Lens
See also [`@set!!`](@ref).
"""
prefermutation
prefermutation(lens::Lens) = Lens!!(lens)
"""
@set!! assignment
Like `Setfield.@set`, but prefer mutation if possible.
# Examples
```jldoctest
julia> using BangBang
julia> mutable struct Mutable
a
b
end
julia> x = orig = Mutable((x=Mutable(1, 2), y=3), 4);
julia> @set!! x.a.x.a = 10;
julia> @assert x.a.x.a == orig.a.x.a == 10
```
"""
:(@set!!)
macro set!!(ex)
Setfield.setmacro(prefermutation, ex, overwrite = true)
end
end # module