Could "==" and "is" be merged? Same for "!=" and "is not" #12456
Replies: 2 comments 3 replies
-
You appear to be confused about the ! and not keywords: the not and ! keywords, which are the same, invert a boolean. Specifically they turn true to false and false to true. No more, no less. The ! found within != does not have anything to do with that. It doesn't invert the expression value itself. But changes the expression itself to check for false instead of true. !true and not true are both false !false and not false are both true
This is the actual anatomy of an if statement. Note that the == true at the end is always implicit and doesn't have to be typed out. So as you can see, first 1 and 2 are compared, returning false, then the not not or ! keyword turns that into true, and then we check for whether or not the outcome of that, is true. Beyond that, it is not possible to merge == and is, because they do different things. There are absolutely situations in which you might want to type the exact same comparison but with the other operator. And removing it would just... well. Break everything. |
Beta Was this translation helpful? Give feedback.
-
if var_name1 is not String Is not the same as if var_name1 != String Because if var_name1 is String Is not the same as if var_name1 == String The former ( The latter ( Be aware that in GDScript a variable can hold a tytpe. For example, let us say I have a script "res://my_script.gd" like this: class_name MyScript If then do this in some other script: var my_variable = load("res://my_script.gd")
if my_variable == MyScript:
print("The types are the same")
var instance = my_variable.new()
if instance is MyScript:
print("The instance is of the type")
if instance != MyScript:
print("The instance is not the same thing as its type")
if my_variable == MyScript:
print("The variable holds the type")
if my_variable is not MyScript:
print("The type of the type is not itself")
if my_variable is Script:
print("Actually the type is a Script")
if instance is not Script:
print("Oh, and the instance is not a Script") I get all those print outs:
We can imagine a world where GDScript would pick up kind of check to do based on the types (e.g. if one of the values is a type check if the other is a value of that type), however that would erode our ability to do the checks shown above. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, "==" and "!=" check for a value. Examples:
Also currently, "is" and "is not" check for a class. Examples:
Yet also currently, "!" and "not" are the same. Examples:
if !var_name1 is String
is the same as
if not var_name1 is String
However,
if var_name1 is not String
is not the same as
if var_name1 != String
Why?
Now, I'm sure there are very established rules in place within Python, etc. as to why this is the case. However, I'm not a coding expert, so I'd like to challenge that in Godot! I wanted to start this as a discussion first as it's potentially a big change.
Could this syntax be merged for the sake of accessibility and readability, as well as for any other reason you, the reader, may think of?
Beta Was this translation helpful? Give feedback.
All reactions