Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The quasi in the example name.alma doesn't need to take a closure #571

Open
masak opened this issue Oct 18, 2021 · 0 comments
Open

The quasi in the example name.alma doesn't need to take a closure #571

masak opened this issue Oct 18, 2021 · 0 comments

Comments

@masak
Copy link
Owner

masak commented Oct 18, 2021

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.

Suggest replacing the final return with this:

my name = expr.name;
return quasi { name };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant