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

Working with iterative methods and binding #336

Open
NagayamaToshiaki opened this issue Nov 30, 2024 · 6 comments
Open

Working with iterative methods and binding #336

NagayamaToshiaki opened this issue Nov 30, 2024 · 6 comments

Comments

@NagayamaToshiaki
Copy link

For example, I would like to write the code like this:

const beatles = [["John", "guitar"], [["Paul", "bass"], ["George", "guitar"], ["Ringo", "drums"]];
if (beatles.some(member => member is [String and const bassist, "bass"])) {
    console.log(`The bassist is ${bassist}.`);
}
  1. Is the syntax right? Will it work?
  2. Can I bind "Paul" to bassist? Does the scope work like this?

Then think of following code:

const beatles = [["John", "guitar"], [["Paul", "bass"], ["George", "guitar"], ["Ringo", "drums"]];
const guitarists = beatles.filter(member => member is [String and const guitarist, "guitar"]);
...

How does the code handle guitarist? Should it throw an error because guitarist is bound twice?
if guitarist is let, is its value "George"?
I do not think the first code will throw an error because binding is executed only once.

@ljharb
Copy link
Member

ljharb commented Nov 30, 2024

The syntax is right, but like all variables, both bassist and guitarist will go out of scope when the containing function returns, so it'll be a useless binding in both snippets, and you'll get a ReferenceError on the undefined bassist variable in the first snippet.

@NagayamaToshiaki
Copy link
Author

@ljharb
Thanks for the response. Let me confirm a couple of things:

  1. The variables bound inside those callbacks are scoped inside its parenthesis, right?
    According to is operator's bindings, normal binding inside if clause is active outside its parenthesis.
    is variables inside callback different?
  2. I thought following revision would work as intended, isn't it?
let bassist;
const beatles = [["John", "guitar"], [["Paul", "bass"], ["George", "guitar"], ["Ringo", "drums"]];
if (beatles.some(member => member is [String and bassist, "bass"])) {
    console.log(`The bassist is ${bassist}.`);
}
  1. Is there any other way to make the code work?

@ljharb
Copy link
Member

ljharb commented Nov 30, 2024

Yes, variables inside a callback are different (or rather, the only variables that are different are in an if clause, and it’s not super clear if that’s viable anyways)

The way to make that code work is const [bassist] = beatles.find((member) => member is [String, 'bass']) ?? []; if (bassist) {

@Jack-Works
Copy link
Member

This code reminds me of another thing.

beatles.some(member => member is [String and const bassist, "bass"])

Did we do the grammar right? Is this member => (member is [String and const bassist, "bass"]) or (member => member) is [String and const bassist, "bass"]?

@NagayamaToshiaki
Copy link
Author

@ljharb Thanks for the response. I understood.

I thought following revision would work as intended, isn't it?

I need the answer for this question.

@Jack-Works

I meant the former: member => (member is [String and const bassist, "bass"]).

@Jack-Works
Copy link
Member

@NagayamaToshiaki Yes! I understand your intention, I mean if we're doing right in the specification so it parses the same as what you mean in this case

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