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

Bang (!) operator #4923

Closed
Chriscbr opened this issue Nov 13, 2023 · 7 comments
Closed

Bang (!) operator #4923

Chriscbr opened this issue Nov 13, 2023 · 7 comments
Assignees
Labels
🛠️ compiler Compiler ✨ enhancement New feature or request 📜 lang-spec-impl Appears in the language spec roadmap 📐 language-design Language architecture

Comments

@Chriscbr
Copy link
Contributor

Chriscbr commented Nov 13, 2023

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:

struct Person {
  name: str;
  age: num;
}

let person = Person.tryParseJson(req.body)!;
log("${person.name} is ${person.age} years old.");

In the example above, Person.tryParseJson returns Person? 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 for foo ?? throws "Unexpected nil" (tracking issue: #3858).

Use Cases

See example above

Implementation Notes

No response

Component

Language Design, Compiler

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.
  • If this issue is labeled needs-discussion, it means the spec has not been finalized yet. Please reach out on the #dev channel in the Wing Slack.
@Chriscbr Chriscbr added ✨ enhancement New feature or request 📐 language-design Language architecture 🛠️ compiler Compiler needs-discussion Further discussion is needed prior to impl labels Nov 13, 2023
@eladb
Copy link
Contributor

eladb commented Nov 14, 2023

I think the example should use tryParseJson() no?

@staycoolcall911 staycoolcall911 added the 📜 lang-spec-impl Appears in the language spec roadmap label Nov 14, 2023
@Chriscbr
Copy link
Contributor Author

Fixed - thanks

Copy link

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.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

@meirdev
Copy link
Contributor

meirdev commented Jan 26, 2024

I think I can implement it, is it ok to send PR?

@staycoolcall911
Copy link
Contributor

@meirdev - yes, please.

The discussion around the syntax is done, so this should be good to work on. I'm removing the needs-discussion label.

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!

@staycoolcall911 staycoolcall911 removed the needs-discussion Further discussion is needed prior to impl label Jan 26, 2024
mergify bot pushed a commit that referenced this issue Feb 7, 2024
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");})())
```
@eladb
Copy link
Contributor

eladb commented Mar 7, 2024

This is implemented, no?

@Chriscbr Chriscbr closed this as completed Mar 7, 2024
@Chriscbr
Copy link
Contributor Author

Chriscbr commented Mar 7, 2024

I believe so - closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛠️ compiler Compiler ✨ enhancement New feature or request 📜 lang-spec-impl Appears in the language spec roadmap 📐 language-design Language architecture
Projects
Archived in project
Development

No branches or pull requests

4 participants