Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] add setproperties!! #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/ConstructionBase.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module ConstructionBase

export setproperties
export setproperties, setproperties!, setproperties!!
export constructorof


# Use markdown files as docstring:
for (name, path) in [
:ConstructionBase => joinpath(dirname(@__DIR__), "README.md"),
Expand Down Expand Up @@ -72,5 +73,26 @@ function setproperties_unknown_field_error(obj, patch)
throw(ArgumentError(msg))
end

function setproperties!(obj, patch::NamedTuple)
_setproperties!(obj, pairs(patch)...)
end

_setproperties!(obj) = obj

function _setproperties!(obj, (key, val), tail...)
Base.setproperty!(obj, key, val)
_setproperties!(obj, tail...)
end

ismutablestruct(x) = ismutablestruct(typeof(x))
Base.@pure ismutablestruct(T::DataType) = T.mutable

function setproperties!!(obj, patch::NamedTuple)
if ismutablestruct(obj)
setproperties!(obj, patch)
else
setproperties(obj, patch)
end
end
Comment on lines +90 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so setproperties!! I had in BangBang.jl haven't implemented the full functionalities I wanted to add. I'm trying to do it in JuliaFolds/BangBang.jl#23

Translating to setproperties!!, it would be something like this:

function setproperties!!(obj, patch::NamedTuple{pnames}) where pnames
    if ismutablestruct(obj) && all(n -> patch[n] isa fieldtype(typeof(obj), n), pnames)
        setproperties!(obj, patch)
    else
        setproperties(obj, patch)
    end
end


end # module
33 changes: 33 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,36 @@ end
@inferred setproperties((a=1, b=2), a=1.0)
@inferred setproperties((a=1, b=2), (a=1.0,))
end

mutable struct TwoMutableInts
a::Int
b::Int
end

struct TwoImmutableInts
a::Int
b::Int
end

@testset "setproperties!" begin
o = TwoMutableInts(1,2)
o2 = @inferred setproperties!(o, (a=10, b=20))
@test o2 isa TwoMutableInts
@test o2.a === 10
@test o2.b === 20

@test o.a === 10
@test o.b === 20
end

@testset "setproperties!!" begin
o = TwoImmutableInts(1,2)
o2 = @inferred setproperties!!(o, (a=10, b=20))
@test o2 === TwoImmutableInts(10, 20)

o = TwoMutableInts(1,2)
o2 = @inferred setproperties!!(o, (a=10, b=20))
@test o2 isa TwoMutableInts
@test o2.a === 10
@test o2.b === 20
end