Skip to content

Commit 4606369

Browse files
authored
Merge pull request #826 from SciML/myb/named
Add @nAmed
2 parents a958405 + 05cbc3c commit 4606369

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-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 isa Symbol ? kw : kw.args[1]) == :name, kws) # don't overwrite `name` kwarg
458+
pushfirst!(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

test/direct.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,53 @@ let
201201
@test isequal(expand_derivatives(D(sin(t) * foo(t))), cos(t) * foo(t) + sin(t) * D(foo(t)))
202202

203203
end
204+
205+
foo(;kw...) = kw
206+
foo(args... ;kw...) = args, kw
207+
pp = :name => :cool_name
208+
209+
@named cool_name = foo()
210+
@test collect(cool_name) == [pp]
211+
212+
@named cool_name = foo(42)
213+
@test cool_name[1] == (42,)
214+
@test collect(cool_name[2]) == [pp]
215+
216+
@named cool_name = foo(42; a = 2)
217+
@test cool_name[1] == (42,)
218+
@test collect(cool_name[2]) == [pp; :a => 2]
219+
220+
@named cool_name = foo(a = 2)
221+
@test collect(cool_name) == [pp; :a => 2]
222+
223+
@named cool_name = foo(;a = 2)
224+
@test collect(cool_name) == [pp; :a => 2]
225+
226+
@named cool_name = foo(name = 2)
227+
@test collect(cool_name) == [:name => 2]
228+
229+
@named cool_name = foo(42; name = 3)
230+
@test cool_name[1] == (42,)
231+
@test collect(cool_name[2]) == [:name => 3]
232+
233+
kwargs = (;name = 3)
234+
@named cool_name = foo(42; kwargs...)
235+
@test cool_name[1] == (42,)
236+
@test collect(cool_name[2]) == [:name => 3]
237+
238+
if VERSION >= v"1.5"
239+
name = 3
240+
@named cool_name = foo(42; name)
241+
@test cool_name[1] == (42,)
242+
@test collect(cool_name[2]) == [:name => name]
243+
@named cool_name = foo(; name)
244+
@test collect(cool_name) == [:name => name]
245+
246+
ff = 3
247+
@named cool_name = foo(42; ff)
248+
@test cool_name[1] == (42,)
249+
@test collect(cool_name[2]) == [pp; :ff => ff]
250+
251+
@named cool_name = foo(;ff)
252+
@test collect(cool_name) == [pp; :ff => ff]
253+
end

0 commit comments

Comments
 (0)