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

Unable to chain null coalescing operator (??) #1875

Closed
eladb opened this issue Mar 21, 2023 · 3 comments · Fixed by #5987
Closed

Unable to chain null coalescing operator (??) #1875

eladb opened this issue Mar 21, 2023 · 3 comments · Fixed by #5987
Assignees
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler 📜 lang-spec-impl Appears in the language spec roadmap

Comments

@eladb
Copy link
Contributor

eladb commented Mar 21, 2023

Community Note

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.

I tried this:

This doesn't work (@Chriscbr brought this up):

let f = (x: str?, y: str?) => {
  let z = x ?? y ?? "hello";
//        ^^^^^^ Expected optional type, found "str"
  print("x is ${z}");
};

This works:

let f = (x: str?, y: str?) => {
  let z = x ?? (y ?? "hello");
  print("x is ${z}");
};

This is also going to be defined in the spec (#436).

I expected this:

To behave the same as JS's Nullish_coalescing operator.

Instead, this happened:

See above.

Component:

Compiler

Wing Version:

No response

Node.js Version:

No response

Platform(s):

No response

Anything else?

No response

@eladb eladb added the 🐛 bug Something isn't working label Mar 21, 2023
@eladb eladb changed the title null Unable to chain null coalescing operator (??) Mar 21, 2023
@github-actions
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!

@github-actions github-actions bot added the Stale label May 22, 2023
@staycoolcall911 staycoolcall911 added the 📜 lang-spec-impl Appears in the language spec roadmap label Jun 28, 2023
@eladb
Copy link
Contributor Author

eladb commented Oct 11, 2023

Here's another use case for this:

if let x = a ?? b { ... }

Both a and b are T? but we get this error:

error: Expected type to be optional, but got "T" instead

@Chriscbr Chriscbr added this to the Winglang Stable Release milestone Mar 14, 2024
@Chriscbr Chriscbr self-assigned this Mar 18, 2024
@mergify mergify bot closed this as completed in #5987 Mar 19, 2024
mergify bot pushed a commit that referenced this issue Mar 19, 2024
Fixes #1875

This fixes the issue by updating the precedent rules so the operation is right-associative. For now, expressions like `a ?? b` or `a ?? b ?? c` still have to return a non-optional value (like Rust's unwrap_or), but we can consider making it more flexible / open in the way that C# or Swift allows if appropriate in the future.

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.61.17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler 📜 lang-spec-impl Appears in the language spec roadmap
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants