Skip to content

Commit 0294f36

Browse files
committed
Handle more general expressions
1 parent 75a3478 commit 0294f36

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/systems/abstractsystem.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,16 @@ function Base.propertynames(sys::AbstractSystem; private=false)
223223
end
224224
end
225225

226-
function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace=false)
226+
Base.getproperty(sys::AbstractSystem, name::Symbol; namespace=false) = getvar(sys, name; namespace=namespace)
227+
getvar(sys, name::Symbol; namespace=false) = getproperty(sys, name)
228+
function getvar(sys::AbstractSystem, name::Symbol; namespace=false)
227229
sysname = nameof(sys)
228230
systems = get_systems(sys)
229231
if isdefined(sys, name)
230232
Base.depwarn("`sys.name` like `sys.$name` is deprecated. Use getters like `get_$name` instead.", "sys.$name")
231233
return getfield(sys, name)
232234
elseif !isempty(systems)
233-
i = findfirst(x->nameof(x)==name,systems)
235+
i = findfirst(x->nameof(x)==name, systems)
234236
if i !== nothing
235237
return namespace ? rename(systems[i], renamespace(sysname,name)) : systems[i]
236238
end
@@ -642,9 +644,16 @@ end
642644
function _config(expr, namespace)
643645
cn = Base.Fix2(_config, namespace)
644646
if Meta.isexpr(expr, :.)
645-
return :($getproperty($(map(cn, expr.args)...); namespace=$namespace))
647+
return :($getvar($(map(cn, expr.args)...); namespace=$namespace))
648+
elseif Meta.isexpr(expr, :function)
649+
def = splitdef(expr)
650+
def[:args] = map(cn, def[:args])
651+
def[:body] = cn(def[:body])
652+
combinedef(def)
646653
elseif expr isa Expr && !isempty(expr.args)
647654
return Expr(expr.head, map(cn, expr.args)...)
655+
elseif Meta.isexpr(expr, :(=))
656+
return Expr(:(=), map(cn, expr.args)...)
648657
else
649658
expr
650659
end
@@ -654,9 +663,9 @@ end
654663
$(SIGNATURES)
655664
656665
Rewrite `@nonamespace a.b.c` to
657-
`getproperty(getproperty(a, :b; namespace = false), :c; namespace = false)`.
666+
`getvar(getvar(a, :b; namespace = false), :c; namespace = false)`.
658667
659-
This is the default behavior of `getproperty`. This should be used when inheriting states from a model.
668+
This is the default behavior of `getvar`. This should be used when inheriting states from a model.
660669
"""
661670
macro nonamespace(expr)
662671
esc(_config(expr, false))
@@ -666,7 +675,7 @@ end
666675
$(SIGNATURES)
667676
668677
Rewrite `@namespace a.b.c` to
669-
`getproperty(getproperty(a, :b; namespace = true), :c; namespace = true)`.
678+
`getvar(getvar(a, :b; namespace = true), :c; namespace = true)`.
670679
"""
671680
macro namespace(expr)
672681
esc(_config(expr, true))

0 commit comments

Comments
 (0)