Skip to content

Commit

Permalink
enable keyword arguments for macros (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel authored Jun 29, 2021
1 parent af1ddfa commit d089597
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ function insert_first_arg(e::Expr, firstarg)
error("You can only use the @. macro and automatic first argument insertion if what follows is of the form `[Module.SubModule.]func`")
end

Expr(head, args[1], args[2], firstarg, args[3:end]...)
if length(args) >= 3 && args[3] isa Expr && args[3].head == :parameters
# macros can have keyword arguments after ; as well
Expr(head, args[1], args[2], args[3], firstarg, args[4:end]...)
else
Expr(head, args[1], args[2], firstarg, args[3:end]...)
end

else
insertionerror(e)
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,23 @@ end
LocalModule.SubModule.@sin
end
@test y7 == LocalModule.SubModule.@sin(3)
end

function kwfunc(y; x = 1)
y * x
end

macro kwmac(exprs...)
:(kwfunc($(esc.(exprs)...)))
end

@testset "keyword arguments" begin

@test 6 == @chain 2 begin
kwfunc(; x = 3)
end

@test 6 == @chain 2 begin
@kwmac(; x = 3)
end
end

0 comments on commit d089597

Please sign in to comment.