macro name(expr) {
if expr ~~ Q.Postfix.Property {
expr = expr.property;
}
assertType(expr, Q.Identifier);
return quasi { expr.name };
}
my info = {
foo: "Bond",
bar: {
baz: "James Bond"
},
};
say(name(info)); # info
say(name(info.foo)); # foo
say(name(info.bar.baz)); # baz
I was re-watching a talk I made in 2018 and I saw this code, and I realized how extravagant return quasi { expr.name } really is. I mean, it works, but at the cost of inserting a property lookup into the macro-expanded code, keeping the expr AST node alive until runtime, just to do that lookup.
I was re-watching a talk I made in 2018 and I saw this code, and I realized how extravagant
return quasi { expr.name }
really is. I mean, it works, but at the cost of inserting a property lookup into the macro-expanded code, keeping theexpr
AST node alive until runtime, just to do that lookup.Suggest replacing the final return with this: