Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable keyword arguments for macros #31

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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