Godot: Ternary conditional operator

Created on 22 May 2015  Â·  28Comments  Â·  Source: godotengine/godot

"If this do this else this" is such a common operation and the reason ternaries exist in almost all modern languages. Them missing is especially egregious in languages with syntactic whitespace. (4 lines for a simple choice.)

Python's ternary syntax is both readable and non-cryptic.

a if test else b
feature proposal gdscript

Most helpful comment

I decided to do the Python variation (apologies to supporters of the other options), since it looks way simpler to implement than all variations that have if before the test. Plus, as @Fiona said, it is the "principle of least programmer work surprise"

All 28 comments

+1, would love to see this implemented. Though - speaking as someone more accustomed to js and php-type syntaxes - I would be thrown off by having one of the possible outcomes declared before the actual testing condition is. Maybe some adaptaion of condition ? a : b (where a is the "if true" option and b is the "else"/"if false") would work?

Does the python thing work as a value? ie can you do "var a = b if test
else c" ?

On Sun, May 24, 2015 at 8:20 PM, jack [email protected] wrote:

+1, would love to see this implemented. Though - speaking as someone more
accustomed to js and php-type syntaxes - I would be thrown off by having
one of the possible outcomes declared before the actual testing condition
is. Maybe some adaptaion of condition ? a : b (where a is the "if true"
option and b is the "else"/"if false") would work?

—
Reply to this email directly or view it on GitHub
https://github.com/okamstudio/godot/issues/1961#issuecomment-105078271.

@punto- Yes that's really what it's primarily for.

will do for 2.1

That's great news!

i had a different suggestion that matches gd script more
if test a else b
if test: a else: b

this is very similar with what you already have in gd script but you can also use it like this
variable = (if test a else b)
variable = (if test: a else: b)
if test variable = a else variable = b
if test: variable = a else: variable = b

this closely mimics gd scripts already existing scripting concepts and still provides same functionality as a shorthand conditional operator

I think we should go with the Python syntax when implementing this, since most of GDScript's syntax is based on python, better stick to it.

I add my voice to this choir
Also agree with @akien-mga , it has to follow python syntax, even if it is a little strange compared to others.

+1 for python syntax

+1 for python syntax

I would like to throw CoffeeScript syntax into the mix:

a = if test then x else y

I'm down for anything as long as it can be used to assign value to a variable or return
var a = x if test else y
return x if test else y

+1 for python syntax, this would be a great addition

GDScript is (largely incorrectly) refered to as a Python-like language. Therefore, deviating from the Python syntax in a core language feature like this would break the principle of least surprise.

@Fiona I agree with you and I even proposed and alternative. Ironically after further consideration I also support Python's version for this ternary operator due to it's simplicity.

I originally proposed a different one which required 6 components
if test : x else y

Bojidar's proposal requieres 6 components
if test then x else y

Python's one only requires 5 components
x if test else y

Quickest one has to be Java but does not really fit with GD Script and is hard to read
test ? x : y

Would love to see any implementation of ternary. Don't have an opinion on syntax so long as there is one to use. :) But, if I had to throw my hat on one, I'd say Java style.

Not critical for the upcoming 2.1, so moving to the next milestone.

2.2 possible? :)

Going to try doing it :smile:

+1 for the Python style for GDScript, though I also like the C/C++/Java-styled operator.

I would prefere "if test : x else y" syntax in trollworkout's post for a mix of least components and semantic easiness. I like to see condition(s) are followed by outcomes and well separated as in a if block which is better for a short syntax for an if block IMO. But any one of them is better than none. :)

I decided to do the Python variation (apologies to supporters of the other options), since it looks way simpler to implement than all variations that have if before the test. Plus, as @Fiona said, it is the "principle of least programmer work surprise"

I preferred the C-style option out of habit but, hey, this will do nicely. Thanks @bojidar-bg !

I don't know if this is the right place to ask, but is there any chance we will get C-style ternary operators? I'm just wondering, since GDscript allows for C-style operators like || && ! << >> & | ^. So I think it would just make sense to add ?: as well. But I cannot code it myself...

@kintrix007 There are no plans to add C-style ternary operators. To make the language easier to learn and reason about, we'd prefer having only one obvious way to write ternary statements :slightly_smiling_face:

@kintrix007 There are no plans to add C-style ternary operators. To make the language easier to learn and reason about, we'd prefer having only one obvious way to write ternary statements 🙂

I am actually searching the same thing, the syntax is not so obvious to me, or to others, I've looked it up several times, so some consistency with other languages would be nice so we don't have to remember another way of doing the same thing basically.

To me and others, I am sure, it's completely the wrong order.

it should be [condition] [true] [false] regardless of the language, in all contexts you check a condition first, before returning the results.

it seems stupid to me to do [true] [condition] [false] and makes little sense to me.

@RevoluPowered GDScript's ternary operator syntax is already consistent with Python's. Python made that design decision over a decade ago, and I doubt it's ever going to change at this point.

There are also cases where [true] [condition] [false] can be easier to read, especially when used as part of a fallback. For example:

var name = player_name if player_name != null else "Unnamed player"

Nesting ternary operators, while not perfect, can be made to be fairly readable:

var name = (
        player_name if player_name != null and player_is_registered
        else "Unregistered player" if player_name != null
        else "Unnamed player"
)

I don't think there's one syntax that will always be more readable than the other. Not to mention the "symbol soup" aspect that comes with those ? and : symbols :slightly_smiling_face:

Was this page helpful?
0 / 5 - 0 ratings