@@ -6,18 +6,20 @@ import Base: typesof, insert!
6
6
7
7
separate_kwargs (args... ; kwargs... ) = (args, kwargs. data)
8
8
9
- function gen_call_with_extracted_types (__module__, fcn, ex0)
9
+ function gen_call_with_extracted_types (__module__, fcn, ex0, kws = Expr[] )
10
10
if isa (ex0, Expr)
11
11
if any (a-> (Meta. isexpr (a, :kw ) || Meta. isexpr (a, :parameters )), ex0. args)
12
12
return quote
13
13
local arg1 = $ (esc (ex0. args[1 ]))
14
14
local args, kwargs = $ separate_kwargs ($ (map (esc, ex0. args[2 : end ])... ))
15
15
$ (fcn)(Core. kwfunc (arg1),
16
- Tuple{typeof (kwargs), Core. Typeof (arg1), map (Core. Typeof, args)... })
16
+ Tuple{typeof (kwargs), Core. Typeof (arg1), map (Core. Typeof, args)... };
17
+ $ (kws... ))
17
18
end
18
19
elseif ex0. head === :call
19
20
return Expr (:call , fcn, esc (ex0. args[1 ]),
20
- Expr (:call , typesof, map (esc, ex0. args[2 : end ])... ))
21
+ Expr (:call , typesof, map (esc, ex0. args[2 : end ])... ),
22
+ kws... )
21
23
elseif ex0. head === :(= ) && length (ex0. args) == 2
22
24
lhs, rhs = ex0. args
23
25
if isa (lhs, Expr)
@@ -95,25 +97,20 @@ of the form "foo=bar" are passed on to the called function as well.
95
97
The keyword arguments must be given before the mandatory argument.
96
98
"""
97
99
function gen_call_with_extracted_types_and_kwargs (__module__, fcn, ex0)
98
- kwargs = Vector{Any} []
100
+ kws = Expr []
99
101
arg = ex0[end ] # Mandatory argument
100
102
for i in 1 : length (ex0)- 1
101
103
x = ex0[i]
102
104
if x isa Expr && x. head === :(= ) # Keyword given of the form "foo=bar"
103
- push! (kwargs, x. args)
105
+ if length (x. args) != 2
106
+ return Expr (:call , :error , " Invalid keyword argument: $x " )
107
+ end
108
+ push! (kws, Expr (:kw , x. args[1 ], x. args[2 ]))
104
109
else
105
110
return Expr (:call , :error , " @$fcn expects only one non-keyword argument" )
106
111
end
107
112
end
108
- thecall = gen_call_with_extracted_types (__module__, fcn, arg)
109
- for kwarg in kwargs
110
- if length (kwarg) != 2
111
- x = string (Expr (:(= ), kwarg... ))
112
- return Expr (:call , :error , " Invalid keyword argument: $x " )
113
- end
114
- push! (thecall. args, Expr (:kw , kwarg[1 ], kwarg[2 ]))
115
- end
116
- return thecall
113
+ return gen_call_with_extracted_types (__module__, fcn, arg, kws)
117
114
end
118
115
119
116
for fname in [:which , :less , :edit , :functionloc ]
0 commit comments