Skip to content

Commit 3ccf6cf

Browse files
authored
Merge pull request #9 from pablosanjose/singlekwdef
Deal with (x; y=0) -> ... pattern
2 parents 0ecb56a + b0ab5d1 commit 3ccf6cf

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/function.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ function splitdef(ex::Expr; throw::Bool=true)
102102
if !haskey(def, :args)
103103
def[:args] = [arg]
104104
elseif !haskey(def, :kwargs)
105-
def[:kwargs] = [arg]
105+
if arg isa Expr && arg.head == :(=)
106+
def[:kwargs] = [:($(Expr(:kw, arg.args...)))]
107+
else
108+
def[:kwargs] = [arg]
109+
end
106110
else
107111
return invalid_def("an invalid block expression as arguments")
108112
end

test/function.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,51 @@ function_form(short::Bool) = string(short ? "short" : "long", "-form")
545545
@test strip_lineno!(c_expr) == strip_lineno!(expr)
546546
end
547547

548+
@testset "(x; y = 0)" begin
549+
f, expr = if short
550+
@audit (x; y = 0) -> (x, y)
551+
else
552+
@audit function (x; y = 0); (x, y) end
553+
end
554+
@test length(methods(f)) == 1
555+
@test f(0) == (0, 0)
556+
@test f(0, y=1) == (0, 1)
557+
558+
# Note: the semi-colon is missing from the expression
559+
d = splitdef(expr)
560+
@test keys(d) == Set([:head, :args, :kwargs, :body])
561+
@test d[:args] == [:x]
562+
@test d[:kwargs] == [Expr(:kw, :y, 0)]
563+
564+
c_expr = combinedef(d)
565+
expr = Expr(:->, Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 0)), :x), Expr(:block, :((x, y))))
566+
expr.head = short ? :-> : :function
567+
@test strip_lineno!(c_expr) == strip_lineno!(expr)
568+
end
569+
570+
@testset "(x; y = 0, _...)" begin
571+
f, expr = if short
572+
@audit (x; y = 0, _...) -> (x, y)
573+
else
574+
@audit function (x; y = 0, _...); (x, y) end
575+
end
576+
@test length(methods(f)) == 1
577+
@test f(0) == (0, 0)
578+
@test f(0, y=1) == (0, 1)
579+
@test f(0, y=1, z=2) == (0, 1)
580+
581+
# Note: the semi-colon is missing from the expression
582+
d = splitdef(expr)
583+
@test keys(d) == Set([:head, :args, :kwargs, :body])
584+
@test d[:args] == [:x]
585+
@test d[:kwargs] == [Expr(:kw, :y, 0), :(_...)]
586+
587+
c_expr = combinedef(d)
588+
expr = Expr(:->, Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 0), :(_...)), :x), Expr(:block, :((x, y))))
589+
expr.head = short ? :-> : :function
590+
@test strip_lineno!(c_expr) == strip_lineno!(expr)
591+
end
592+
548593
@testset "Expr(:block, :x, :y)" begin
549594
expr = Expr(:->, Expr(:block, :x, :y), Expr(:block, :((x, y))))
550595
expr.head = short ? :-> : :function

0 commit comments

Comments
 (0)