-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Rewrite proposal to use AssignmentExpression #65
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are a lot of things here that need dedicated discussions before getting merged. I suggest you to split them into smaller PRs
/** @type {ResultConstructor} */ | ||
class Result { | ||
/** @type {TryResultConstructor} */ | ||
class TryResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to change to TryResult
? Result is an understandable type with the same name as in other languages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Result seems too generic in the JavaScript world and it's very ambiguous what it's actually for. It could easily create name collisions in user space. TryResult
is much less likely to collide and its purpose is clearer, though still somewhat ambiguous.
- **`ok`**: A boolean indicating whether the expression executed successfully. | ||
- **`error`**: The error thrown during execution, or `undefined` if no error occurred. | ||
- **`value`**: The data returned from the execution, or `undefined` if an error occurred. | ||
AssignmentExpression[In, Yield, Await] : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll need to deeply study our syntax use-case and read similar proposals to see if this syntax is correct or not. Any sources inspired you to write it in this way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it from the ECMA262 spec. I also updated the spec.emu
file, which looks a lot nicer than this does and integrates into the existing spec.
You can see what it looks like here: https://arlen22.github.io/proposal-safe-assignment-operator/
The ECMA262 parser grammar actually makes it extremely simple to implement the try keyword for all single expressions. It also is extremely simple to catch any errors that are thrown.
Syntax
Runtime Evaluation (simplified slightly)
Explanation
The definition for the "single expression" is called the Assignment Expression. It is so named because it is the expression that returns the result of assigning a value to a variable, allowing
x=y=z=5
. It is used in many places throughout ECMA262 as it is the highest form of a single expression.The Assignment Expression is found in many places
((x = 4, y = x=6) => x=5)(a=4);
if
,while
, anddo while
conditions.for
and all expressions infor(;;)
yield yield yield
.In keeping with the examples above,
All of this is based on my technical understanding of the ECMA262 spec, which is quite limited, but hopefully sufficient.
A note on commas
I use the term "single expressions" to differentiate from multiple expressions separated by commas.
However, when surrounded by parentheses, these multiple expressions are child to the single expression created by the parentheses. Therefore the try keyword may protect multiple expressions, and return the result of the last expression, using the parentheses. This is inherently how parentheses work. They combine multiple expressions into a single expression that returns the last value. So if needed, the try expression can be wrapped in parentheses.
In the ECMA262, multiple comma-separated expressions are only allowed in a few places, such as parentheses, return, and throw. They are really not allowed anywhere else without parentheses, whereas the Assignment Expression is used just about everywhere, and includes the parentheses case, so there was literally no point in trying to find ways to allow commas. It just makes everything a whole lot easier.