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

Consider type annotations as pattern syntax #309

Open
kaleidawave opened this issue Nov 24, 2023 · 2 comments
Open

Consider type annotations as pattern syntax #309

kaleidawave opened this issue Nov 24, 2023 · 2 comments

Comments

@kaleidawave
Copy link

With type annotations potentially being added, I think it would be worth considering whether the patterns could be the same as type annotations. Type annotations would cover primitive literal types 1, "some string", true etc. and objects { tag: "a", data }, arrays etc. It would also allow referencing/reusability through the use of interfaces and type aliases.

This would allow this feature to be adopted quicker as existing type annotations could be used in patterns. It also reduces the work for type checkers to do narrowing in the matcher body.

Additional I think that the matching block should be the same AST as arrow function bodies. It should also copy the return syntax being the result of the body rather than introducing different semantics (or a forth return like keyword).

The result being something like

match (res) {
    when (number) => 5,
    when ({ tag: "number", data }) => data + 2,
    when (InterfaceX) => {
        for (const x of res.items) {
            if (x === res.expect) {
                return x;
            }
        }
        return null
    },
}
@ljharb
Copy link
Member

ljharb commented Nov 24, 2023

That proposal is stage 1 with no clear path to ever reaching stage 2; if we block this proposal on that one we’ll never get pattern matching.

@littledan
Copy link
Member

These two proposals are peers at Stage 1. Not sure how to make judgements like "no clear path to ever reaching stage 2" -- I am still hopeful.

That said, the core concept of type annotations is that types are treated as comments at runtime, so I don't think it's a good fit for pattern matching, which is supposed to do stuff.

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