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

Ternary Operator '?' #403

Closed
lavender-programming opened this issue Oct 29, 2023 · 3 comments · Fixed by #574
Closed

Ternary Operator '?' #403

lavender-programming opened this issue Oct 29, 2023 · 3 comments · Fixed by #574
Labels
enhancement New feature or request

Comments

@lavender-programming
Copy link

In a lot languages, the ones I can think of are C++, and C# a '?' question mark can be used to do ternary operators.
C++/C#: result = (a >= 45) ? "true": "false";
QB64?: Print (a >= 45) ? "true": "false"
It could make simple if statements easier, and stuff.
The options could be uses of function too like: clamp(1, a, 10) or something.
Just a more compact, and easier way to do smaller conditional things.

@lavender-programming lavender-programming added the enhancement New feature or request label Oct 29, 2023
@SteveMcNeill
Copy link
Member

Lots of issues in the proposed example, which would make it impossible.

PRINT (A >= 45) ? "true": "false"

With the above, you have to keep in mind that the quotation mark is shorthand for PRINT. Try a simple ? "Hello World" sometime.

Issue 2, is that the colon is a command separator. PRINT: PRINT: PRINT <-- those colons there let us stack all the 3 commands on one line.

Which then means that issue 3 is simply the quote left dangling all by itself at the end there. "false"

I'd think this type of thing might would be best served via an user function:

FUNCTION Ternary$ (equation AS FLOAT, truth AS STRING, falseness AS STRING)
    Ternary$ = falseness
    IF equation THEN Ternary$ = truth
END FUNCTION

Which then could go into your example code as: PRINT"; Ternary( a >= 45, "true", "false")

@lavender-programming
Copy link
Author

This make sense, and I forgot about the print short-cut, and colon separator. Adding a Ternary Operator doesn't work like any other QB64 syntax, so it would have to be sort-of hard-coded in the lexer/compiler.

@a740g
Copy link
Contributor

a740g commented Oct 30, 2023

VB and friends have the IIf function. However, both the true-part and the false-part are evaluated in those languages which IMO can cause subtle issues that are hard to catch.

If something like _IIF were ever to be implemented in QB64-PE, I would prefer it to be a C-like short-circuiting operator rather than a function.

Also see #211.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

3 participants