This package lets you:
- Change
getproperty
togetfield
viaFields(x)
- Change
getproperty
togetindex
viaIndexes(x)
(most useful forAbstractDict{Symbol, Any}
). - Similarly,
Fields
, andIndexes
change the behavior ofsetproperty!
. - Replace items in an expression with properties from a
src
via@with src expr
. - Join together properties of different objects via
JoinProps
.
@with src expr
- Every valid identifier
x
inexpr
gets changed tohasproperty(src, :x) ? src.x : x
z = 3
result = @with (x = 1, y = 2) begin
x + y + z
end
result == 6
Example:
a = (x = 1, y = 2)
b = (y = 3, z = 4)
j = JoinProps(a, b)
j.x == 1
j.y == 2 # non-unique props are taken from the first argument that has it
j.z == 4
struct A
x::Int
end
Base.getproperty(::A, x::Symbol) = "hello!"
item = A(1)
f_item = Fields(a)
item.x == "hello!"
f_item.x == 1
d = Dict(:x => 1, :y => 2)
Indexes(d).y == 2
@with
, Fields
, Indexes
, and JoinProps
play nicely together:
result = @with JoinProps(Fields(A(10)), a, b, Indexes(Dict(:twenty => 20))) begin
x + y + z + twenty
end
result == 36
setproperty!
, e.g. thing.x = 1
, is supported if the underlying data structure supports mutation.
Indexes(x)
:setproperty!
-->setindex!
Fields(x)
:setproperty!
-->setfield!
JoinProps(x)
:setproperty!
-->setproperty!
on the first instance of the prop. You cannot create new props.
Indexes(d).z = 3
d[:z] == 3
This package borrows ideas from StatsModels.jl, DataFramesMeta.jl, StaticModules.jl, and StatsPlots.jl, which are all fantastic.