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=== nothing ? arg_name :
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
@@ -415,34 +418,34 @@ end
415
418
splitarg(arg)
416
419
417
420
Match function arguments (whether from a definition or a function call) such as
418
- `x::Int=2` and return `(arg_name, arg_type, is_splat, default)`. `arg_name` and
419
- `default` are `nothing` when they are absent. For example:
421
+ `x::Int=2` and return `(arg_name, arg_type, is_splat, default)`. `arg_name`,
422
+ `arg_type`, and ` default` are `nothing` when they are absent. For example:
420
423
421
424
```julia
422
425
julia> map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end])
423
426
4-element Array{Tuple{Symbol,Symbol,Bool,Any},1}:
424
- (:a, :Any , false, 2)
427
+ (:a, nothing , false, 2)
425
428
(:x, :Int, false, :nothing)
426
429
(:y, :Any, false, nothing)
427
- (:args, :Any , true, nothing)
430
+ (:args, nothing , true, nothing)
428
431
```
429
432
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
+ @assert default != = nothing " splitarg cannot handle `nothing` as a default. Use a quoted `nothing` if possible. (MacroTools#35)"
443
438
else
444
- return ( splitvar ( arg_expr2) ... , is_splat, nothing )
439
+ arg_expr2 = arg_expr
445
440
end
441
+ is_splat = @capture (arg_expr2, arg_expr3_... )
442
+ is_splat || (arg_expr3 = arg_expr2)
443
+ (arg_name, arg_type) = (@match arg_expr3 begin
444
+ :: T_ => (nothing , T)
445
+ name_:: T_ => (name, T)
446
+ x_ => (x, nothing )
447
+ end ):: NTuple{2,Any} # the pattern `x_` matches any expression
448
+ return (arg_name, arg_type, is_splat, default)
446
449
end
447
450
448
451
0 commit comments