11export @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
33
44"""
55 assoc!(d, k, v)
406406`combinearg` is the inverse of [`splitarg`](@ref).
407407"""
408408function 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 )
410413 a2 = is_splat ? Expr (:... , a) : a
411414 return default === nothing ? a2 : Expr (:kw , a2, default)
412415end
@@ -415,34 +418,34 @@ end
415418 splitarg(arg)
416419
417420Match 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:
420423
421424```julia
422425julia> map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end])
4234264-element Array{Tuple{Symbol,Symbol,Bool,Any},1}:
424- (:a, :Any , false, 2)
427+ (:a, nothing , false, 2)
425428 (:x, :Int, false, :nothing)
426429 (:y, :Any, false, nothing)
427- (:args, :Any , true, nothing)
430+ (:args, nothing , true, nothing)
428431```
429432
430433See also: [`combinearg`](@ref)
431434"""
432435function 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)"
443438 else
444- return ( splitvar ( arg_expr2) ... , is_splat, nothing )
439+ arg_expr2 = arg_expr
445440 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)
446449end
447450
448451
0 commit comments