Macros to fill keyword arguments

Here’s one solution – the trick is that I think you need QuoteNode to make the symbols it receives stay symbols in the output:

macro p3(args...)
    sym = QuoteNode.(args)
    :( $NamedTuple{($(sym...),)}(($(args...),)) ) |> esc
end

using NamedTupleTools # for this constructor

A = rand(1:9, 3,4)
dims = 1

@macroexpand @p3(dims)

sum(A; @p3(dims)...) # needs the ; and ...

If you paste this into the REPL it should work find without the esc, but if it were in a module, then it would look for dims there, not in your scope, if you left this out. The reason for writing $NamedTuple is to put this back in the module’s scope, so that the caller need not have loaded it.

2 Likes