This package lets you:
- Change
getpropertytogetfieldviaFields(x) - Change
getpropertytogetindexviaIndexes(x)(most useful forAbstractDict{Symbol, Any}). - Similarly,
Fields, andIndexeschange the behavior ofsetproperty!. - Replace items in an expression with properties from a
srcvia@with src expr. - Join together properties of different objects via
JoinProps.
@with src expr- Every valid identifier
xinexprgets changed tohasproperty(src, :x) ? src.x : x
z = 3
result = @with (x = 1, y = 2) begin
x + y + z
end
result == 6Example:
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 == 4struct A
x::Int
end
Base.getproperty(::A, x::Symbol) = "hello!"
item = A(1)
f_item = Fields(a)
item.x == "hello!"
f_item.x == 1d = 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 == 36setproperty!, 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] == 3This package borrows ideas from StatsModels.jl, DataFramesMeta.jl, StaticModules.jl, and StatsPlots.jl, which are all fantastic.