-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add adapt_structure macro * add tests * clarify doc * move struct outside of testset for old scope rules
- Loading branch information
1 parent
a2f9aa4
commit 1b19759
Showing
3 changed files
with
33 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
Adapt.@adapt_structure T | ||
Define a method `adapt_structure(to, obj::T)` which calls `adapt_structure` on each field | ||
of `obj` and constructs a new instance of `T` using the default constuctor `T(...)`. | ||
""" | ||
macro adapt_structure(T) | ||
names = fieldnames(Core.eval(__module__, T)) | ||
quote | ||
function Adapt.adapt_structure(to, obj::$(esc(T))) | ||
$(esc(T))($([:(Adapt.adapt_structure(to, obj.$name)) for name in names]...)) | ||
end | ||
end | ||
end |
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