-
Notifications
You must be signed in to change notification settings - Fork 39
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
Define functional setters using Setfield #191
Conversation
Codecov Report
@@ Coverage Diff @@
## master #191 +/- ##
==========================================
- Coverage 84.87% 84.63% -0.25%
==========================================
Files 12 12
Lines 410 423 +13
==========================================
+ Hits 348 358 +10
- Misses 62 65 +3
Continue to review full report at Codecov.
|
Cool, so this now is purely functional, with no mutation, right? |
Yep, it's purely functional. Internally, Setfield uses something called lens which is invented by a Haskeller: https://github.com/ekmett/lens |
Alright, yes, then lets do this. Do you have to rebase now that #190 is merged, or do we merge this right away? |
It looks like it has no conflict so it can be merged as-is? But let me know if you want me to rebase. (I just restarted the CI for now.) |
The diff here on the PR looks weird: it looks like it will add |
Oh, I wasn't looking at the diff. I rebased the PR. |
function Setfield.set(spec::ObjectLike, ::PropertyLens{name}, value) where name | ||
params = copy(getparams(spec)) | ||
params[keytype(params)(name)] = _maybeparams(value) | ||
@set getparams(spec) = params | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be written as @set getparams(spec)[keytype(params)(name)] = value
if Base had setindex
for Dict
...: JuliaLang/julia#33495
Thanks, I really like this now! If you have time, maybe add some docs as another PR? |
Will do. Nice thing about Setfield is that it works with other constructs. Things like obj = (
plt1 = @vlplot(...),
plots = (
@vlplot(...),
@vlplot(...),
@vlplot(...),
),
)
@set obj.plt1.mark = :point
@set obj.plots[2].mark = :line
@set last(obj.plots).mark = :line should work now. |
Ok, that is pretty amazing! |
With this PR, you can do:
This is built on top of #190 and will close #168.