-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Note: I have to exclude get/set from ambiguity checks again. So, a part of commit 52218dd had to be reverted.
- Loading branch information
Showing
11 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Internals | ||
|
||
```@autodocs | ||
Modules = [BangBang, BangBang.NoBang] | ||
Modules = [BangBang, BangBang.NoBang, BangBang.SetfieldImpl] | ||
Public = false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module SetfieldImpl | ||
|
||
export @set!!, prefermutation | ||
|
||
using ..BangBang: setproperty!!, setindex!! | ||
using Setfield: ComposedLens, | ||
ConstIndexLens, | ||
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) | ||
|
||
const SupportedIndexLens = Union{ConstIndexLens,DynamicIndexLens,IndexLens} | ||
|
||
indicesfor(lens::IndexLens, _) = lens.indices | ||
indicesfor(::ConstIndexLens{I}, _) where {I} = I | ||
indicesfor(lens::DynamicIndexLens, obj) = lens.f(obj) | ||
|
||
Setfield.set(obj, lens::Lens!!{<:SupportedIndexLens}, value) = | ||
setindex!!(obj, value, indicesfor(lens, obj)...) | ||
|
||
""" | ||
prefermutation(lens::Lens) :: Lens | ||
See also [`@set!!`](@ref). | ||
""" | ||
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 | ||
``` | ||
""" | ||
macro set!!(ex) | ||
Setfield.setmacro(prefermutation, ex, overwrite = true) | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module TestSetfield | ||
|
||
include("preamble.jl") | ||
|
||
mutable struct Mutable | ||
a | ||
b | ||
end | ||
|
||
@testset begin | ||
x = orig = Mutable((x=Mutable(1, 2), y=3), 4); | ||
@set!! x.a.x.a = 10; | ||
@test x.a.x.a == orig.a.x.a == 10 | ||
end | ||
|
||
end # module |