wing
wing copied to clipboard
Add ternaries (b ? c : d)
We'd like to add support for ternaries.
- [ ] Also add to the winglang reference
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!
Keep
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!
It would be pretty cool if we could reuse if / else / elif
in the expression position (like Rust and Swift allows) since IMO it's slightly more readable than the archaic ?: ternary. See this Swift proposal for more details: https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md. What do others think?
I like the idea of turning if
statements into expressions (like rust). I also think that given the extensive use of the ?
char when dealing with optionals we might run into grammar challenges or just confusion because ?
is usually related to optionals:
let x = (a ?? b?) ? c?.y : 1; // Explain this
Personally I find ternaries readable and dislike if
expressions. I also dislike variable shadowing though so I'm probably just a luddite ¯\(ツ)/¯
I agree with @MarkMcCulloh I find the if
expressions to make things look very busy but thats just preference.
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!
The syntactic ambiguity raised by @yoav-steinberg is valid - fortunately, I think the visual similarity between expr ? ...
and expr? ...
should be addressed by removing the unwrap/condition expression syntax (https://github.com/winglang/wing/issues/5942).
The "traditional" ternary syntax a ? b : c
is already in a lot of languages today (C, C++, Java, JavaScript, PHP, Ruby, Swift, Perl) so I think going forward with this design makes the most sense. One of our design goals is to make Wing to feel familiar to people who have used other popular languages.
Type checking this expression can be modeled after the logic already used for type checking simple if/else statements in the compiler.
I'll mark this as a good issue if anyone wants to pick this up!
Hey Chris, I want to pick this up.
Thanks @Pahul-Sidhu! I've assigned the issue to you. You'll want to look into adding ternaries to grammar.js and then incorporate it into the rest of the parser here. Let me know if I can provide other pointers or help with debugging any confusing errors.
Thank you