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

why are parens required? #55

Closed
ljharb opened this issue Jan 20, 2025 · 5 comments
Closed

why are parens required? #55

ljharb opened this issue Jan 20, 2025 · 5 comments

Comments

@ljharb
Copy link

ljharb commented Jan 20, 2025

in the "Parentheses Required for Object Literals in try Expressions" section, it says it's mirroring arrow functions - but the only reason arrows have that restriction is because without parens, it'll be a block. Can you do:

try {
  statementOne();
  statementTwo();
}

? if not, then having this restriction is unnecessary and confusing.

@arthurfiorette
Copy link
Owner

arthurfiorette commented Jan 20, 2025

This decision was also to give an early hint that it's not the start of a try/catch block.

try { // is this a try/catch block or a try-expression of a literal object?
const [ok, err, val] = try ({ field: 'value' })

try {
  something()
} catch {}

It also follows this idea:

try {
  { field: 'value' } // label inside an empty code block
} catch {}

try {
  ({ field: 'value' }) // object literal
} catch {}

Try with blocks doesn't make sense in this case because blocks do not evaluate a result. The following code, If you do not generate a value, scoping problems and/or error handling are not an issue following this principle.

What should be the result value in the below code?

const result = try {
  statementOne();
  statementTwo();
}

That seems to be something that https://github.com/tc39/proposal-do-expressions aims to solve and not this proposal.

@ljharb
Copy link
Author

ljharb commented Jan 20, 2025

I agree try with blocks doesn't make sense.

I think it's reasonable if there's a grammar ambiguity to require the parens here, but then that's the reason, not "consistency with arrows".

@arthurfiorette
Copy link
Owner

https://github.com/arthurfiorette/proposal-try-statements?tab=readme-ov-file#parenthesis-required-for-object-literals

@Arlen22
Copy link
Collaborator

Arlen22 commented Jan 22, 2025

Arrow function bodies also follow this pattern.

@ljharb
Copy link
Author

ljharb commented Jan 22, 2025

@Arlen22 yes, that mention in the readme prompted the issue in the first place.

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