Let’s say I have this function
import Dates: DateTime
function myfun1(; ena::Int=1, offsettime::DateTime, entrytime::DateTime, dio::Int)
return nothing
end
with the parameters being
:($(Expr(:parameters, :($(Expr(:kw, :(ena::Int), 1))), :(offsettime::Main.DateTime), :(entrytime::Main.DateTime), :(dio::Int))))
I wanted to create a macro that will automate writing offsetime
, and entrytime
.
When I do it one by one it works:
macro recvtime1()
return :($(esc(:offsettime))::DateTime)
end
macro recvtime2()
return :($(esc(:entrytime))::DateTime)
end
function myfun2(; ena=1, @recvtime1, @recvtime2, dio=2)
return nothing
end
But when I do it together it complains for what I think is an automatically generated parenthesis:
macro recvtime()
return :($(esc(:offsettime))::DateTime, $(esc(:entrytime))::DateTime)
end
The error:
julia> function myfun3(; ena=1, @recvtime, dio=2)
return nothing
end
ERROR: syntax: invalid keyword argument syntax "(offsettime::Main.DateTime, entrytime::Main.DateTime)" around REPL[47]:1
Stacktrace:
[1] top-level scope
@ REPL[47]:1
Is there a way I can adapt @recvtime
such that myfun3
could work as is ?
@macroexpand
indeed shows the extra parenthesis. I think if I can get them out it should work but I don’t see how.
julia> @macroexpand @recvtime
:((offsettime::Main.DateTime, entrytime::Main.DateTime))
I know I can pass to the macro the whole keyword parameter list and just return an Expr(:parameters, )
with adding the two keywords, but I prefer not to operate on that level.