-
Notifications
You must be signed in to change notification settings - Fork 14
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
About expressions in the header of a block #28
Comments
I am in favor of banning |
Hi @sffc!
I don't agree with this -- because these questions are about scope, the syntactic placement of the source text matters. If you define the semantics according to a rewrite that moves the placement of a declaration, you can change the scope in ways that don't line up with the intuitions conveyed by the original syntax. |
@bakkot I think the sweet spot here is to make function parameter expressions a scope where |
An issue I see with saying "keyword X is not allowed in default expressions" is that we are essentially creating a restricted subset of JavaScript for use in this one particular construction; for perpetuity, we will need to consider whether any new language keywords should be included in this peculiar subset allowed in default expressions. Assigning the default expressions to be first-class members of the inner scope seems like a clean solution.
The expressions in the header of a function are written in the same place in the code as the function definition. So to me, I don't see how this would be "moving" the placement of the code.
The |
They moved from the parameter list to the body. And JavaScript already does distinguish those. For example: function f(thunk = () => window) {
var window = "inner variable";
return thunk;
}
let thunk = f();
thunk() === window // true!
I agree, except there's also a third choice, which is to say that this isn't a scope where |
That seems fine to me, although if I'm understanding correctly that's mostly a point about the implementation details of how it would be banned and establishing an expectation for future proposals, rather than an observable difference in semantics, right? That is, the practical effect (in the absence of other future proposals) would still be "you can't use I still personally prefer banning
It's already possible to see the There's an open PR changing this behavior, but it's still not going to put |
No, it's only disallowed in the context of parameter default expressions. In other contexts it's allowed, for example: function f() {
let x = do {
var y = 41;
1
};
console.log(x + y) // 42
} |
That's what I meant, yeah: "you can't use var in parameters in do-expressions" (I guess I got those in the the wrong order, and should be "you can't use var in do-expressions in parameters"). I think we're on the same page. (Incidentally, it's not just parameter defaults which can introduce expressions: function f({ [do { var x; 'a'}]: a }) {
// look ma, no defaults!
} ) |
Ah, cool! 👍 |
Good catch, thank you! |
Follow-up from today's TC39 discussion:
It seems to me that if you have the code,
then that should be semantically the same as,
return, break, var, etc. all just become part of the function body.
This makes it clear what the default expression turns into, and avoids the edge cases that you presented today and that will likely continue to pop up as long as new features are added to the language.
The text was updated successfully, but these errors were encountered: