-
Notifications
You must be signed in to change notification settings - Fork 196
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
Bang (!) operator #4923
Comments
I think the example should use |
Fixed - thanks |
Hi, This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. |
I think I can implement it, is it ok to send PR? |
@meirdev - yes, please. The discussion around the syntax is done, so this should be good to work on. I'm removing the We might need to refactor it a bit in the future once we work on #3858, but that shouldn't hold your work on this issue. I assigned you on this issue so others won't work on it simultaneously. Let us know if you have any questions. Thanks! |
I added the bang operator as described in #4923. `throw` is a statement, so `x!` cannot be compiled to: `x ?? throw 'nil'`, It is also not enough to use block: `x ?? { throw 'nil' }` (syntax error). The only way is to use a function: `x ?? (() => throw 'nil')()`, and that's how I implemented it. More precisely, `x!` becoming: ``` (x??(()=>{throw new Error("Unexpected nil");})()) ```
This is implemented, no? |
I believe so - closing |
Feature Spec
The bang operator can be used to tersely unwrap an optional value, throwing an error if it is nil. This is notably different from TypeScript's bang operator (
!
), which silently allows code to continue running if is undefined or null.For example:
In the example above,
Person.tryParseJson
returnsPerson?
because parsing the string as Json and converting the Json to Person may fail. If it fails, the "!" indicates that the code will automatically throw an error.foo!
is syntax sugar forfoo ?? throws "Unexpected nil"
(tracking issue: #3858).Use Cases
See example above
Implementation Notes
No response
Component
Language Design, Compiler
Community Notes
The text was updated successfully, but these errors were encountered: