Skip to content

Commit a4b8132

Browse files
committed
Add at named
1 parent a958405 commit a4b8132

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,5 +295,6 @@ export build_function
295295
export @register
296296
export modelingtoolkitize
297297
export @variables, @parameters
298+
export @named
298299

299300
end # module

src/systems/abstractsystem.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,41 @@ function Base.show(io::IO, sys::AbstractSystem)
425425
end
426426
return nothing
427427
end
428+
429+
function _named(expr)
430+
if !(expr isa Expr && expr.head === :(=) && expr.args[2].head === :call)
431+
throw(ArgumentError("expression should be of the form `sys = foo(a, b)`"))
432+
end
433+
name, call = expr.args
434+
435+
has_kw = false
436+
if length(call.args) >= 2 && call.args[2] isa Expr
437+
# canonicalize to use `:parameters`
438+
if call.args[2].head === :kw
439+
call.args[2] = Expr(:parameters, Expr(:kw, call.args[2].args...))
440+
has_kw = true
441+
elseif call.args[2].head === :parameters
442+
has_kw = true
443+
end
444+
end
445+
446+
if !has_kw
447+
param = Expr(:parameters)
448+
if length(call.args) == 1
449+
push!(call.args, param)
450+
else
451+
insert!(call.args, 2, param)
452+
end
453+
end
454+
455+
kws = call.args[2].args
456+
457+
if !any(kw->kw.args[1] == :name, kws) # don't overwrite `name` kwarg
458+
push!(kws, Expr(:kw, :name, Meta.quot(name)))
459+
end
460+
:($name = $call)
461+
end
462+
463+
macro named(expr)
464+
esc(_named(expr))
465+
end

0 commit comments

Comments
 (0)