Macro for implicitly defined NamedTuples

Thanks for the comments :slight_smile: And good catch with the multiple evaluations!

I knew I saw that somewhere but somehow forgot completely that Meta exists :sweat_smile:

If need be, we can easily support both syntax versions with a simple check:

macro imp2(expr2)
    expr = expr2
    if expr.head == :tuple
        expr = expr.args[1]
    end
    expr.head == :parameters || error("I don't understand this: $(dump(expr;maxdepth=4))")
    bindings = Expr(:block, (Expr(:(=), e.args[1], e.args[2]) for e in expr.args)...)
    body = Expr(:tuple, (Expr(:(=), e.args[1], e.args[1]) for e in expr.args)...)
    return esc(Expr(:let, bindings, body))
end
1 Like