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

Add safe comparison operator (===) #31331

Closed
dalexeev opened this issue Aug 12, 2019 · 8 comments
Closed

Add safe comparison operator (===) #31331

dalexeev opened this issue Aug 12, 2019 · 8 comments

Comments

@dalexeev
Copy link
Member

dalexeev commented Aug 12, 2019

Godot version:
3.1.1.stable.official

Issue description:
At this point, the == operator can only compare values of the same type:

var x = 2
print(x == true) # Error: Invalid operands 'int' and 'bool' in operator '=='

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.

var res = my_func()
if res is String and res == "":
    do_something()

I propose to add the operator ===, which allows you to compare the value of any type with the value of any type. Herewith:

  1. If A and B are values of different types, then A === B is always false.
  2. If A and B are of the same type, then A === B is always equal to A == B.

Then the condition in the example will be a little easier:

var res = my_func()
if res === "":
    do_something()

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 "" or 0, for example.
UPD 2. Although now I think it's better to change the behavior of the == operator. (#26375)

@dalexeev
Copy link
Member Author

var x = 2
print(x == "2") # Error: Invalid operands 'int' and 'String' in operator '=='

In Python:

>>> 2 == '2'
False

However, no errors.

@KoBeWi
Copy link
Member

KoBeWi commented Aug 12, 2019

Related: #26375

@aaronfranke
Copy link
Member

aaronfranke commented Aug 12, 2019

var res = my_func()
if res is bool and res == false:
    do_something()

Alternatively:

var res: bool = my_func()
if !res:
    do_something()

You should not be using == false or == true because it is bad practice and produces confusing logical statements that could otherwise be much simpler.

@dalexeev
Copy link
Member Author

  1. This does not always work as expected:
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'
  1. 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 "" or 0, for example.

@aaronfranke
Copy link
Member

@dalexeev That's really weird, I can confirm this behavior, but that's definitely a bug. @vnen You should check this out, it seems that typed GDScript is not working at all here.

@dalexeev
Copy link
Member Author

static func eq(a, b) -> bool: # a === b # a ?= b # a =?= b
    return typeof(a) == typeof(b) and a == b

@zkmark
Copy link

zkmark commented Sep 25, 2023

It's 2023, couldn't they add that very important functionality in godot 4?

@dalexeev
Copy link
Member Author

There is is_same() global function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants