Skip to content

Commit 137d3d1

Browse files
committed
Support implicit name unpack in at extend
1 parent 3034c9f commit 137d3d1

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/systems/model_parsing.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
237237
if mname == Symbol("@components")
238238
parse_components!(exprs, comps, dict, body, kwargs)
239239
elseif mname == Symbol("@extend")
240-
parse_extend!(exprs, ext, dict, body, kwargs)
240+
parse_extend!(exprs, ext, dict, mod, body, kwargs)
241241
elseif mname == Symbol("@variables")
242242
parse_variables!(exprs, vs, dict, mod, body, :variables, kwargs)
243243
elseif mname == Symbol("@parameters")
@@ -372,24 +372,35 @@ function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false)
372372
end
373373
end
374374

375-
function parse_extend!(exprs, ext, dict, body, kwargs)
375+
function parse_extend!(exprs, ext, dict, mod, body, kwargs)
376376
expr = Expr(:block)
377377
varexpr = Expr(:block)
378378
push!(exprs, varexpr)
379379
push!(exprs, expr)
380380
body = deepcopy(body)
381381
MLStyle.@match body begin
382382
Expr(:(=), a, b) => begin
383-
vars = nothing
384383
if Meta.isexpr(b, :(=))
385384
vars = a
386385
if !Meta.isexpr(vars, :tuple)
387386
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
388387
end
389388
a, b = b.args
390-
extend_args!(a, b, dict, expr, kwargs, varexpr)
391-
vars, a, b
389+
else
390+
if (model = getproperty(mod, b.args[1])) isa Model
391+
_vars = keys(get(model.structure, :variables, Dict()))
392+
_vars = union(_vars, keys(get(model.structure, :parameters, Dict())))
393+
_vars = union(_vars,
394+
map(first, get(model.structure, :components, Vector{Symbol}[])))
395+
vars = Expr(:tuple)
396+
append!(vars.args, collect(_vars))
397+
else
398+
error("Cannot infer the exact `Model` that `@extend $(body)` refers." *
399+
" Please specify the names that it brings into scope by:" *
400+
" `@extend a, b = oneport = OnePort()`.")
401+
end
392402
end
403+
extend_args!(a, b, dict, expr, kwargs, varexpr)
393404
ext[] = a
394405
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
395406
push!(expr.args, :($a = $b))

test/model_parsing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ l15 0" stroke="black" stroke-width="1" stroke-linejoin="bevel" fill="none"></pat
101101
@parameters begin
102102
C, [unit = u"F"]
103103
end
104-
@extend v, i = oneport = OnePort(; v = 0.0)
104+
@extend oneport = OnePort(; v = 0.0)
105105
@icon "https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg"
106106
@equations begin
107107
D(v) ~ i / C

0 commit comments

Comments
 (0)