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

Improve macros for inherited enums? #15

Open
overlookmotel opened this issue May 7, 2024 · 7 comments
Open

Improve macros for inherited enums? #15

overlookmotel opened this issue May 7, 2024 · 7 comments

Comments

@overlookmotel
Copy link

Last bit of unfinished business after oxc-project/oxc#3115...

That PR introduced squashed/inherited enums. The least ergonomic API is for matching, which requires a set of match_* macros e.g.:

match prop_key {
    PropertyKey::StaticIdentifier(id) => id.do_stuff(),
    PropertyKey::PrivateIdentifier(id) = id.do_stuff(),
    match_expression!(PropertyKey) => prop_key.to_expression().do_stuff(),
}

Would this be better?

match_with_subtypes!(prop_key, {
    PropertyKey::StaticIdentifier(id) => id.do_stuff(),
    PropertyKey::PrivateIdentifier(id) = id.do_stuff(),
    @subtype::PropertyKey::Expression(expr) => expr.do_stuff(),
});

This reduces boilerplate and makes the code easier to read. But is it too much magic?

@rzvxa You had strong opinions on this matter. Would you like to chime in?

@overlookmotel
Copy link
Author

overlookmotel commented May 7, 2024

The above match_with_subtypes! example would expand to:

match prop_key {
    PropertyKey::StaticIdentifier(id) => id.do_stuff(),
    PropertyKey::PrivateIdentifier(id) = id.do_stuff(),
    expr @ (
        PropertyKey::BooleanLiteral(_)
        | PropertyKey::NullLiteral(_)
        /* ...and all the other Expression variants */
    ) => {
        let expr = expr.to_expression();
        expr.do_stuff()
    }
}

@rzvxa
Copy link
Collaborator

rzvxa commented May 8, 2024

@rzvxa You had strong opinions on this matter. Would you like to chime in?

I've mulled it over for a couple of days, I still believe it adds to the code noise and makes stuff harder to follow. But if we are committing to this, Let's do it all the way!

@Boshen Boshen transferred this issue from oxc-project/oxc May 18, 2024
@Boshen Boshen removed their assignment May 18, 2024
@overlookmotel
Copy link
Author

overlookmotel commented Jul 13, 2024

@hyf0 suggested an alternative on Discord:

Extend existing match_* macros to allow this:

match expr {
    match_member_expression!(member_expr @ Expression) => member_expr.do_stuff()
}

which would expand to:

match expr {
    _temp @ (
        Expression::ComputedMemberExpression(_)
        | Expression::StaticMemberExpression(_)
        | Expression::PrivateFieldExpression(_)
    ) => {
        let member_expr = _temp.to_member_expression();
        member_expr.do_stuff()
    }
}

I think this is really good! @rzvxa you like?

@rzvxa
Copy link
Collaborator

rzvxa commented Jul 13, 2024

Yes, I like what I'm seeing. I'm glad we gave this issue a bit of time because it seems like a more natural way of expressing the same thing we wanted in the original comment.

Small nit, Shouldn't we expand to this?

match expr {
    member_expr @ (
        Expression::ComputedMemberExpression(_)
        | Expression::StaticMemberExpression(_)
        | Expression::PrivateFieldExpression(_)
    ) => {
        let member_expr = member_expr.to_member_expression();
        member_expr.do_stuff()
    }
}

@overlookmotel
Copy link
Author

Small nit, Shouldn't we expand to this?

Yes, you're right. That's better.

@overlookmotel
Copy link
Author

Argh I forgot I tried this before, and it doesn't work! It'd have to be:

match expr {
    match_member_expression!( member_expr @ Expression => member_expr.do_stuff() )
}

And macros do not support expanding to match arms.

@rzvxa
Copy link
Collaborator

rzvxa commented Jul 13, 2024

I was about to point out the match arms issue😅

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

3 participants