If we could generate a function f
as below, then you could just splat the NamedTuple
into the keyword parameters of f
subtotal = (price=12.99, shipping=3.99)
f(; price, shipping) = price + shipping
f(; subtotal ...) # 16.98
I feel like I’m almost there:
julia> subtotal = (price=12.99, shipping=3.99)
(price = 12.99, shipping = 3.99)
julia> function genfunc(nt, body)
local props = propertynames(nt)
f = :( (; $(props...) )-> $body )
g = Meta.eval(f)
Base.invokelatest(g; nt...)
end
genfunc (generic function with 1 method)
julia> genfunc(subtotal, quote
shipping + price
end)
16.98