Skip to content

Commit

Permalink
Restrict patch to NamedTuple (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf authored Oct 1, 2019
1 parent f3cf74a commit d186a75
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

ConstructionBase is a very lightwight package, that provides primitive functions for construction of objects:
```julia
setproperties(obj::MyType, patch)
setproperties(obj::MyType, patch::NamedTuple)
constructorof(MyType)
```
These functions can be overloaded and doing so provides interoperability the following packages:
Expand Down
8 changes: 4 additions & 4 deletions src/ConstructionBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function assert_hasfields(T, fnames)
end

"""
setproperties(obj, patch)
setproperties(obj, patch::NamedTuple)
Return a copy of `obj` with attributes updates accoring to `patch`.
Expand Down Expand Up @@ -111,15 +111,15 @@ S("A", 2, "cc")
# Overloading
**WARNING** The signature `setproperties(obj::MyType; kw...)` should never be overloaded.
Instead `setproperties(obj::MyType, patch)` should be overloaded.
Instead `setproperties(obj::MyType, patch::NamedTuple)` should be overloaded.
"""
function setproperties end

function setproperties(obj; kw...)
setproperties(obj, (;kw...))
end

@generated function setproperties(obj, patch)
@generated function setproperties(obj, patch::NamedTuple)
assert_hasfields(obj, fieldnames(patch))
args = map(fieldnames(obj)) do fn
if fn in fieldnames(patch)
Expand All @@ -134,7 +134,7 @@ end
)
end

@generated function setproperties(obj::NamedTuple, patch)
@generated function setproperties(obj::NamedTuple, patch::NamedTuple)
# this function is only generated to force the following check
# at compile time
assert_hasfields(obj, fieldnames(patch))
Expand Down

0 comments on commit d186a75

Please sign in to comment.