wing icon indicating copy to clipboard operation
wing copied to clipboard

Add ternaries (b ? c : d)

Open staycoolcall911 opened this issue 2 years ago • 9 comments

We'd like to add support for ternaries.

  • [ ] Also add to the winglang reference

staycoolcall911 avatar Oct 04 '22 09:10 staycoolcall911

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[bot] avatar Dec 06 '22 06:12 github-actions[bot]

Keep

staycoolcall911 avatar Dec 06 '22 07:12 staycoolcall911

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[bot] avatar Feb 07 '23 06:02 github-actions[bot]

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?

Chriscbr avatar Feb 07 '23 18:02 Chriscbr

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

yoav-steinberg avatar Mar 23 '23 09:03 yoav-steinberg

Personally I find ternaries readable and dislike if expressions. I also dislike variable shadowing though so I'm probably just a luddite ¯\(ツ)

MarkMcCulloh avatar Mar 23 '23 11:03 MarkMcCulloh

I agree with @MarkMcCulloh I find the if expressions to make things look very busy but thats just preference.

hasanaburayyan avatar Mar 23 '23 11:03 hasanaburayyan

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[bot] avatar May 25 '23 06:05 github-actions[bot]

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!

Chriscbr avatar May 13 '24 18:05 Chriscbr

Hey Chris, I want to pick this up.

Pahul-Sidhu avatar Jul 25 '24 17:07 Pahul-Sidhu

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.

Chriscbr avatar Jul 25 '24 19:07 Chriscbr

Thank you

Pahul-Sidhu avatar Jul 25 '24 19:07 Pahul-Sidhu