Skip to content

Commit f7428eb

Browse files
committed
✨ Late properties aliasing.
1 parent 02fd6f2 commit f7428eb

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Framework/method_macro.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,24 @@ function eval_dependency(mod, xp, ctx, err)
495495
check_component_type_or_instance(xp, dep, ctx, err)
496496
end
497497
end
498+
499+
# Call later to append aliases.
500+
# TODO: this is only tested by the above client package yet. Test within framework tests.
501+
macro alias(a, b, V)
502+
a, b = Meta.quot.((a, b))
503+
quote
504+
$alias_property!($a, $b, $V)
505+
end
506+
end
507+
export @alias
508+
function alias_property!(a, b, V)
509+
# Extract original.
510+
(A, B) = property_space_type.((a, b), (V,))
511+
(Pa, Pb) = super.((A, B))
512+
(na, nb) = last_in_path.((a, b))
513+
fn_a = read_property(Pa, Val(na)) # Checked.
514+
fn_a! = possible_write_property(Pa, Val(na))
515+
set_read_property!(Pb, nb, fn_a)
516+
isnothing(fn_a!) && return
517+
set_write_property!(Pb, nb, fn_a!)
518+
end

src/components/main.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ include("./class.jl")
3636

3737
# XXX: this is how we may define this component now. Use to work on `exposed_data`.
3838
@class_component species Species s
39+
@alias species.number S
40+
@alias S richness
41+
@alias S species.richness
3942
export Species
4043

44+
# Extra aliases required by the audience.
45+
# S, richness, species.richness
46+
4147
# Helpers.
4248
# include("./macros_keywords.jl")
4349
# include("./shared.jl")

src/dedicate_framework_to_model.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ macro propspace(path)
8282
end
8383
end
8484

85+
# Convenience macro to alias properties.
86+
macro alias(a, b)
87+
quote
88+
F.@alias($a, $b, $Internal)
89+
end
90+
end
91+
8592
# ==========================================================================================
8693
# The above defines var"get_a.b" method names for nested properties
8794
# to avoid possible ambiguity with `get_a_b`.

0 commit comments

Comments
 (0)