-
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
The ?? operator cannot return an optional #5944
Comments
Totally agree with the confusion! We already have an issue so I'll close this as a duplicate -- I've gone ahead and added the other issue to our stability milestone (#1875) |
Reopening this because the use case is a bit different from just chaining I expected that when let x: str? = y ?? z; The use case is: if let x = y ?? z {
log("either y or z is defined");
} else {
log("neither y nor z are defined");
} Additionally, I'd like this to work: let a: str = (y ?? z)! |
Right, this is slightly different than #1875. In rust we differentiate the two use cases as: I'm somewhat hesitant for using the same operator for both cases, since it feels weird and lacks explicity of what you're trying to do. Alternate syntax:
|
Not sure I see the problem with using the same operator. The strong types will not allow developers to make mistakes here. |
I've been think about this a bit since the discussion in #5987. I can see how returning a non-optional could feel weird coming from Rust, if you associate The way I'd reason about If someone writes For comparison, there is a decent reason for restricting |
+1 on what @Chriscbr said :-) |
Ok, I'm somewhat convinced, lets make sure there's a test that covers:
But to nitpick on the subject, how about supporting (not really):
|
Fixes #5944 This improves the type checking for expressions of the form `a ?? b` in two ways: 1. `b` is allowed to be an optional. So for example, if `x` and `y` are both `num?`, then `x ?? y` is allowed, and its type of the resulting expression is `num?` (reflecting that you may still end up with a nil). 2. The subtyping is more permissive. For example, suppose you have two classes where `Derived` is a derived class from `Base`. (In other words, `Derived` is a subtype of `Base`). All of these type combinations are allowed: a. Optional(Base) ?? Optional(Derived) = Optional(Base) b. Optional(Base) ?? Derived = Base c. Optional(Derived) ?? Optional(Base) = Optional(Base) d. Optional(Derived) ?? Base = Base ## 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)*.
Congrats! 🚀 This was released in Wing 0.61.22. |
Hi, This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. |
Hi, This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. |
I tried this:
Wrote this code:
This happened:
Got this error:
It was on the part after the
??
operator.Even explicitely defining that s3 is optional didn't help:
This code still produced the same error:
I expected this:
The code to compile an s3 to equal nil.
Is there a workaround?
But it is uglier, and requires s3 to be set to
var
Anything else?
I know it seems like a very small issue with a workaround, but for someone new to Wing, this can be the point that they lose interest and get back to a familiar language
Wing Version
0.61.1
Node.js Version
No response
Platform(s)
MacOS
Community Notes
The text was updated successfully, but these errors were encountered: