@@ -19,34 +19,26 @@ default_kwargs(f::Function, ::Vararg{<:Type}; kwargs...) = (; kwargs...)
1919Automatically define a `default_kwargs` method for a given function. This macro should
2020be applied before a function definition:
2121```
22- @define_default_kwargs astypes = true function f(args ...; kwargs...)
22+ @define_default_kwargs function f(arg1::T1, arg2::T2, ...; kwargs...)
2323 ...
2424end
2525```
26- If `astypes = true` then the `default_kwargs` method is defined in the
27- type domain with respect to `args`, i.e.
26+ The defined `default_kwargs` method takes the form
2827```
29- default_kwargs(::typeof(f), arg::T; kwargs...) # astypes = false
30- default_kwargs(::typeof(f), arg::Type{<:T}; kwargs...) # astypes = true
28+ default_kwargs(::typeof(f), arg1::T1, arg2::T2, ...; kwargs...)
3129```
30+ i.e. the function signature mirrors that of the function signature of `f`.
3231"""
33- macro define_default_kwargs (args... )
34- kwargs = (;)
35- for opt in args
36- if @capture (opt, key_ = val_)
37- kwargs = merge (kwargs, NamedTuple {(key,)} ((val,)))
38- elseif opt === last (args)
39- return default_kwargs_macro (opt; kwargs... )
40- else
41- throw (ArgumentError (" Unknown expression object" ))
42- end
43- end
32+ macro define_default_kwargs (function_def)
33+ return default_kwargs_macro (function_def)
4434end
4535
46- function default_kwargs_macro (function_def; astypes = true )
36+ function default_kwargs_macro (function_def)
4737 if ! isdef (function_def)
4838 throw (
49- ArgumentError (" The @define_default_kwargs macro must be followed by a function definition" )
39+ ArgumentError (
40+ " The @define_default_kwargs macro must be followed by a function definition"
41+ ),
5042 )
5143 end
5244
@@ -78,14 +70,7 @@ function default_kwargs_macro(function_def; astypes=true)
7870 return kw
7971 end
8072
81- # Promote to the type domain if wanted
8273 new_ex[:args ] = convert (Vector{Any}, ex[:args ])
83- if astypes
84- new_ex[:args ] = map (new_ex[:args ]) do arg
85- @capture (arg, name_:: T_ )
86- return :($ (name):: Type{<:$T} )
87- end
88- end
8974
9075 new_ex[:name ] = :(ITensorNetworks. default_kwargs)
9176 new_ex[:args ] = pushfirst! (new_ex[:args ], :(:: typeof ($ (esc (ex[:name ])))))
0 commit comments