Skip to content

Commit 75a3478

Browse files
committed
Default to inheritance
1 parent fc6926e commit 75a3478

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,6 @@ export simplify, substitute
184184
export build_function
185185
export modelingtoolkitize
186186
export @variables, @parameters
187-
export @named, @nonamespace
187+
export @named, @nonamespace, @namespace
188188

189189
end # module

src/systems/abstractsystem.jl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ for prop in [
181181
end
182182
end
183183

184-
Setfield.get(obj::AbstractSystem, l::Setfield.PropertyLens{field}) where {field} = getfield(obj, field)
184+
Setfield.get(obj::AbstractSystem, ::Setfield.PropertyLens{field}) where {field} = getfield(obj, field)
185185
@generated function ConstructionBase.setproperties(obj::AbstractSystem, patch::NamedTuple)
186186
if issubset(fieldnames(patch), fieldnames(obj))
187187
args = map(fieldnames(obj)) do fn
@@ -223,7 +223,7 @@ function Base.propertynames(sys::AbstractSystem; private=false)
223223
end
224224
end
225225

226-
function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace=true)
226+
function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace=false)
227227
sysname = nameof(sys)
228228
systems = get_systems(sys)
229229
if isdefined(sys, name)
@@ -232,7 +232,7 @@ function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace=true)
232232
elseif !isempty(systems)
233233
i = findfirst(x->nameof(x)==name,systems)
234234
if i !== nothing
235-
return namespace ? rename(systems[i],renamespace(sysname,name)) : systems[i]
235+
return namespace ? rename(systems[i], renamespace(sysname,name)) : systems[i]
236236
end
237237
end
238238

@@ -639,11 +639,12 @@ macro named(expr)
639639
esc(_named(expr))
640640
end
641641

642-
function _nonamespace(expr)
642+
function _config(expr, namespace)
643+
cn = Base.Fix2(_config, namespace)
643644
if Meta.isexpr(expr, :.)
644-
return :($getproperty($(map(_nonamespace, expr.args)...); namespace=false))
645+
return :($getproperty($(map(cn, expr.args)...); namespace=$namespace))
645646
elseif expr isa Expr && !isempty(expr.args)
646-
return Expr(expr.head, map(_nonamespace, expr.args)...)
647+
return Expr(expr.head, map(cn, expr.args)...)
647648
else
648649
expr
649650
end
@@ -654,9 +655,21 @@ $(SIGNATURES)
654655
655656
Rewrite `@nonamespace a.b.c` to
656657
`getproperty(getproperty(a, :b; namespace = false), :c; namespace = false)`.
658+
659+
This is the default behavior of `getproperty`. This should be used when inheriting states from a model.
657660
"""
658661
macro nonamespace(expr)
659-
esc(_nonamespace(expr))
662+
esc(_config(expr, false))
663+
end
664+
665+
"""
666+
$(SIGNATURES)
667+
668+
Rewrite `@namespace a.b.c` to
669+
`getproperty(getproperty(a, :b; namespace = true), :c; namespace = true)`.
670+
"""
671+
macro namespace(expr)
672+
esc(_config(expr, true))
660673
end
661674

662675
"""

0 commit comments

Comments
 (0)