1
1
export @esc , isexpr, isline, iscall, rmlines, unblock, block, inexpr, namify, isdef,
2
- longdef, shortdef, @expand , makeif, prettify, combinedef, splitdef, splitarg
2
+ longdef, shortdef, @expand , makeif, prettify, combinedef, splitdef, splitarg, combinearg
3
3
4
4
"""
5
5
assoc!(d, k, v)
406
406
`combinearg` is the inverse of [`splitarg`](@ref).
407
407
"""
408
408
function combinearg (arg_name, arg_type, is_splat, default)
409
- a = arg_name=== nothing ? :(:: $arg_type ) : :($ arg_name:: $arg_type )
409
+ @assert arg_name != = nothing || arg_type != = nothing
410
+ a = arg_name=== nothing ? :(:: $arg_type ) :
411
+ arg_type== :Any && is_splat ? arg_name : # see #177 and julia#43625
412
+ :($ arg_name:: $arg_type )
410
413
a2 = is_splat ? Expr (:... , a) : a
411
414
return default === nothing ? a2 : Expr (:kw , a2, default)
412
415
end
@@ -430,19 +433,21 @@ julia> map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end])
430
433
See also: [`combinearg`](@ref)
431
434
"""
432
435
function splitarg (arg_expr)
433
- splitvar (arg) =
434
- (@match arg begin
435
- :: T_ => (nothing , T)
436
- name_:: T_ => (name, T)
437
- x_ => (x, :Any )
438
- end ):: NTuple{2,Any} # the pattern `x_` matches any expression
439
- (is_splat = @capture (arg_expr, arg_expr2_... )) || (arg_expr2 = arg_expr)
440
- if @capture (arg_expr2, arg_ = default_)
441
- @assert default != = nothing " splitarg cannot handle `nothing` as a default. Use a quoted `nothing` if possible. (MacroTools#35)"
442
- return (splitvar (arg)... , is_splat, default)
436
+ if @capture (arg_expr, arg_expr2_ = default_)
437
+ # This assert will only be triggered if a `nothing` literal was somehow spliced into the Expr.
438
+ # A regular `nothing` default value is a `Symbol` when it gets here. See #178
439
+ @assert default != = nothing " splitarg cannot handle `nothing` as a default. Use a quoted `nothing` if possible. (MacroTools#35)"
443
440
else
444
- return ( splitvar ( arg_expr2) ... , is_splat, nothing )
441
+ arg_expr2 = arg_expr
445
442
end
443
+ is_splat = @capture (arg_expr2, arg_expr3_... )
444
+ is_splat || (arg_expr3 = arg_expr2)
445
+ (arg_name, arg_type) = (@match arg_expr3 begin
446
+ :: T_ => (nothing , T)
447
+ name_:: T_ => (name, T)
448
+ x_ => (x, :Any )
449
+ end ):: NTuple{2,Any} # the pattern `x_` matches any expression
450
+ return (arg_name, arg_type, is_splat, default)
446
451
end
447
452
448
453
0 commit comments