-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add safe comparison operator (===) #31331
Comments
var x = 2
print(x == "2") # Error: Invalid operands 'int' and 'String' in operator '==' In Python:
However, no errors. |
Related: #26375 |
Alternatively:
You should not be using |
var x = "abc"
var res: bool = x
print(res) # prints 'abc'
print(res == true) # Error: Invalid operands 'String' and 'bool' in operator '==' var x = null
var res: String = x
print(res) # prints 'Null'
print(res.length()) # Error: Invalid call. Nonexistent function 'length' in base 'Nil'
|
static func eq(a, b) -> bool: # a === b # a ?= b # a =?= b
return typeof(a) == typeof(b) and a == b |
It's 2023, couldn't they add that very important functionality in godot 4? |
There is |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Godot version:
3.1.1.stable.official
Issue description:
At this point, the
==
operator can only compare values of the same type:This is absolutely the right decision, but sometimes it can be inconvenient. For example, if a function returns a value of an arbitrary type, you have to check both the type and the value.
I propose to add the operator
===
, which allows you to compare the value of any type with the value of any type. Herewith:A
andB
are values of different types, thenA === B
is alwaysfalse
.A
andB
are of the same type, thenA === B
is always equal toA == B
.Then the condition in the example will be a little easier:
P. S. And still need the opposite operator, of course.
P. P. S. You can think about the syntax of the operator.
UPD.
my_func()
can return a value of any type (e.g. a string or a boolean). And in this case, I need to check that the function returned false, not""
or0
, for example.UPD 2. Although now I think it's better to change the behavior of the
==
operator. (#26375)The text was updated successfully, but these errors were encountered: