Skip to content

Commit

Permalink
Merge pull request #9 from pablosanjose/singlekwdef
Browse files Browse the repository at this point in the history
Deal with (x; y=0) -> ... pattern
  • Loading branch information
oxinabox authored Apr 14, 2020
2 parents 0ecb56a + b0ab5d1 commit 3ccf6cf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ function splitdef(ex::Expr; throw::Bool=true)
if !haskey(def, :args)
def[:args] = [arg]
elseif !haskey(def, :kwargs)
def[:kwargs] = [arg]
if arg isa Expr && arg.head == :(=)
def[:kwargs] = [:($(Expr(:kw, arg.args...)))]
else
def[:kwargs] = [arg]
end
else
return invalid_def("an invalid block expression as arguments")
end
Expand Down
45 changes: 45 additions & 0 deletions test/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,51 @@ function_form(short::Bool) = string(short ? "short" : "long", "-form")
@test strip_lineno!(c_expr) == strip_lineno!(expr)
end

@testset "(x; y = 0)" begin
f, expr = if short
@audit (x; y = 0) -> (x, y)
else
@audit function (x; y = 0); (x, y) end
end
@test length(methods(f)) == 1
@test f(0) == (0, 0)
@test f(0, y=1) == (0, 1)

# Note: the semi-colon is missing from the expression
d = splitdef(expr)
@test keys(d) == Set([:head, :args, :kwargs, :body])
@test d[:args] == [:x]
@test d[:kwargs] == [Expr(:kw, :y, 0)]

c_expr = combinedef(d)
expr = Expr(:->, Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 0)), :x), Expr(:block, :((x, y))))
expr.head = short ? :-> : :function
@test strip_lineno!(c_expr) == strip_lineno!(expr)
end

@testset "(x; y = 0, _...)" begin
f, expr = if short
@audit (x; y = 0, _...) -> (x, y)
else
@audit function (x; y = 0, _...); (x, y) end
end
@test length(methods(f)) == 1
@test f(0) == (0, 0)
@test f(0, y=1) == (0, 1)
@test f(0, y=1, z=2) == (0, 1)

# Note: the semi-colon is missing from the expression
d = splitdef(expr)
@test keys(d) == Set([:head, :args, :kwargs, :body])
@test d[:args] == [:x]
@test d[:kwargs] == [Expr(:kw, :y, 0), :(_...)]

c_expr = combinedef(d)
expr = Expr(:->, Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 0), :(_...)), :x), Expr(:block, :((x, y))))
expr.head = short ? :-> : :function
@test strip_lineno!(c_expr) == strip_lineno!(expr)
end

@testset "Expr(:block, :x, :y)" begin
expr = Expr(:->, Expr(:block, :x, :y), Expr(:block, :((x, y))))
expr.head = short ? :-> : :function
Expand Down

0 comments on commit 3ccf6cf

Please sign in to comment.