Skip to content

Commit

Permalink
Add setproperties!!
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Nov 5, 2019
1 parent 99bb0be commit 435790b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/BangBang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export @!,
pushfirst!!,
rmul!!,
setindex!!,
setproperties!!,
setproperty!!,
singletonof,
splice!!
Expand Down
28 changes: 19 additions & 9 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,22 @@ possible(::typeof(_setindex!), ::C, ::V, ::K) where {C <: AbstractDict, V, K} =
promote_type(valtype(C), V) <: valtype(C)

"""
setproperty!!(value, name, x)
setproperties!!(value, patch::NamedTuple)
setproperties!!(value; patch...)
# Examples
```jldoctest
julia> using BangBang
julia> setproperty!!((a=1, b=2), :b, 3)
julia> setproperties!!((a=1, b=2); b=3)
(a = 1, b = 3)
julia> struct Immutable
a
b
end
julia> setproperty!!(Immutable(1, 2), :b, 3)
julia> setproperties!!(Immutable(1, 2); b=3)
Immutable(1, 3)
julia> mutable struct Mutable
Expand All @@ -272,23 +273,32 @@ julia> mutable struct Mutable
julia> s = Mutable(1, 2);
julia> setproperty!!(s, :b, 3)
julia> setproperties!!(s; b=3)
Mutable(1, 3)
julia> s
Mutable(1, 3)
```
"""
setproperty!!(value, name, x) = may(_setproperty!, value, name, x)
setproperties!!(value, patch) = may(setproperties!, value, patch)
setproperties!!(value; patch...) = setproperties!!(value, (; patch...))

function _setproperty!(value, name, x)
setproperty!(value, name, x)
function setproperties!(value, patch)
for (k, v) in pairs(patch)
setproperty!(value, k, v)
end
return value
end

pure(::typeof(_setproperty!)) = NoBang.setproperty
possible(::typeof(_setproperty!), x, ::Any, ::Any) = ismutablestruct(x)
pure(::typeof(setproperties!)) = NoBang.setproperties
possible(::typeof(setproperties!), x, ::Any) = ismutablestruct(x)

"""
setproperty!!(value, name::Symbol, x)
An alias of `setproperty!!(value, (name=x,))`.
"""
setproperty!!(value, name::Symbol, x) = setproperties!!(value, (; name => x))

"""
materialize!!(dest, x)
Expand Down

0 comments on commit 435790b

Please sign in to comment.