generated from tc39/template-for-proposals
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Open
Labels
BrainstormDiscussions about possible new featuresDiscussions about possible new features
Description
This issue is primarily for condensing technical ideas into practical ones. If you want to discuss something you see here, feel free to open a new issue.
The try operator
-
like any operator,
- it takes a value and gives a value
- it can be used pretty much anywhere
- its result can be ignored
- it cannot contain a statement
-
like the
yieldoperator,- it takes the highest precedence without a comma
- it can contain itself, yield, or anything else
-
like the
tryblock,- it catches anything that throws inside it, including the
awaitandyieldkeywords. - it transparently supports generator and async functions.
- it clearly indicates whether or not the code it protects ran to completion.
- it catches anything that throws inside it, including the
-
To maintain equivalence,
try {}: It must catch any exception thrown by a specific section of code.catch (e) {}: It must allow different code to execute if an exception was caught and provide a reference to the exception thrown.finally {}: It must allow code to execute regardless of whether or not an exception was caught.- It must not allow any value from that section of code to change or spoof these constraints.
-
this gives us two more invariants
- it must return a result that clearly indicates whether this is the normal completion (perhaps a boolean). Checking undefined is not enough since both normal and abrupt completions may return undefined.
- it must not flatten its result. If the expression it protects returns or throws a
Result, it must still wrap it in a newResultregardless. Otherwise, the user could not distinguish between returningResult(false)and actually throwing, or between throwingResult(true)and actually returning.
/**
* ( try something())
* (<operator> <expression>)
*/
operator try(expression) {
try {
return TryResult.ok(exec(expression));
} catch (inner) {
return TryResult.error(inner);
}
}does this
const a = try something()
const [[ok, err, val]] = [try something()]
const [ok, err, val] = try something()
files.map(file => try JSON.parse(file))
yield try something()
try yield something()
try await something()
try a instanceof b
(try a) instanceof TryResult
const a = try try try try try try 1Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BrainstormDiscussions about possible new featuresDiscussions about possible new features