From 200ba959f4d60b58db2e063c7ef13e30a8bc6717 Mon Sep 17 00:00:00 2001 From: jkosata Date: Wed, 23 Aug 2023 21:57:32 +0200 Subject: [PATCH] expanding macrocalls and blocks in with_kw --- src/Parameters.jl | 4 ++++ test/runtests.jl | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/Parameters.jl b/src/Parameters.jl index 47945ff..319c27a 100644 --- a/src/Parameters.jl +++ b/src/Parameters.jl @@ -330,6 +330,10 @@ function with_kw(typedef, mod::Module, withshow=true) if typedef.head==:tuple # named-tuple withshow==false && error("`@with_kw_noshow` not supported for named tuples") return with_kw_nt(typedef, mod) + elseif typedef.head == :macrocall + return with_kw(macroexpand(mod, typedef), mod, withshow) + elseif typedef.head == :block + return with_kw(typedef.args[2], mod, withshow) elseif typedef.head != :struct error("""Only works on type-defs or named tuples. Make sure to have a space after `@with_kw`, e.g. `@with_kw (a=1,) diff --git a/test/runtests.jl b/test/runtests.jl index eb7a3a2..f072e32 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -724,3 +724,10 @@ end @test_throws ErrorException global b34 = 1 ## this test is dependent on Julia version, leave it: # @test_warn "WARNING: redefinition of constant b34. This may fail, cause incorrect answers, or produce other errors." global b34 = 4.0 + +macro structmacro() + return quote struct mystruct; myfield::Int; end end +end + +# triggers a with_kw call on :macrocall and :block +@with_kw @structmacro