jw3126 / Setfield.jl

Update deeply nested immutable structs.
Other
167 stars 17 forks source link

Allow interpolation of properties #168

Closed phipsgabler closed 2 years ago

phipsgabler commented 2 years ago

This comes up in some VarName use cases in DynamicPPL (cf. https://github.com/TuringLang/AbstractPPL.jl/issues/46).

I simply add a case to check property access with is_interpolation.

Any ideas/requirements for more test cases?

jw3126 commented 2 years ago

Nice! That looks handy. What happens if you try obj.$(f(x) ? An example with a nested attribute access could also be added.

phipsgabler commented 2 years ago

You mean like that?

julia> @lens(_.a[1].$(rand([:x, :y])))
(@lens _.a[1].x)

julia> @lens(_.a[1].$(rand([:x, :y])))
(@lens _.a[1].x)

julia> @lens(_.a[1].$(rand([:x, :y])))
(@lens _.a[1].y)

julia> @macroexpand @lens(_.a[1].$(rand([:x, :y])))
:((identity)((Setfield.compose)((Setfield.PropertyLens){:a}(), (Setfield.IndexLens)((1,)), (Setfield.PropertyLens){rand([:x, :y])}())))
jw3126 commented 2 years ago

I think I will merge this one. If we decide to tweak the parser for https://github.com/TuringLang/AbstractPPL.jl/pull/54 that can be a separate PR. Sounds good @phipsgabler ?

jw3126 commented 2 years ago

Ah sry @phipsgabler, can you bump the patch version?