Skip to content

Commit

Permalink
Merge pull request #168 from phipsgabler/phg/interpolation
Browse files Browse the repository at this point in the history
Allow interpolation of properties
  • Loading branch information
jw3126 authored Feb 19, 2022
2 parents bff69d5 + 8fdfb7f commit 25f5f5a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Setfield"
uuid = "efcf1570-3423-57d1-acb7-fd33fddbac46"
version = "0.8.1"
version = "0.8.2"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
20 changes: 15 additions & 5 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,17 @@ function parse_obj_lenses(ex)
lens = :($IndexLens($index))
end
elseif @capture(ex, front_.property_)
property isa Union{Symbol,String} || throw(ArgumentError(
string("Error while parsing :($ex). Second argument to `getproperty` can only be",
"a `Symbol` or `String` literal, received `$property` instead.")
))
obj, frontlens = parse_obj_lenses(front)
lens = :($PropertyLens{$(QuoteNode(property))}())
if property isa Union{Symbol,String}
lens = :($PropertyLens{$(QuoteNode(property))}())
elseif is_interpolation(property)
lens = :($PropertyLens{$(esc(property.args[1]))}())
else
throw(ArgumentError(
string("Error while parsing :($ex). Second argument to `getproperty` can only be",
"a `Symbol` or `String` literal, received `$property` instead.")
))
end
elseif @capture(ex, f_(front_))
obj, frontlens = parse_obj_lenses(front)
lens = :($FunctionLens($(esc(f))))
Expand Down Expand Up @@ -231,6 +236,11 @@ julia> t = ("one", "two")
julia> set(t, (@lens _[1]), "1")
("1", "two")
julia> # Indices are always evaluated in external scope; for properties, you can use interpolation:
n, i = :a, 10
@lens(_.\$n[i, i+1])
(@lens _.a[10, 11])
```
"""
Expand Down
7 changes: 7 additions & 0 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ end
@test @lens($lbc)== lbc
@test @lens(_.a $lbc) == @lens(_.a) lbc
@test @lens(_.a $lbc _[1] $lbc) == @lens(_.a) lbc @lens(_[1]) lbc

# property interpolation
name = :a
fancy(name, suffix) = Symbol("fancy_", name, suffix)
@test @lens(_.$name) == @lens(_.a)
@test @lens(_.x[1, :].$name) == @lens(_.x[1, :].a)
@test @lens(_.x[1, :].$(fancy(name, ""))) == @lens(_.x[1, :].fancy_a✨)
end

end

2 comments on commit 25f5f5a

@jw3126
Copy link
Owner Author

@jw3126 jw3126 commented on 25f5f5a Feb 19, 2022

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/54979

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.2 -m "<description of version>" 25f5f5a0e3f4cbc6f8c44955fb7fb0981b961e55
git push origin v0.8.2

Please sign in to comment.