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

Allow for comparing variables of different types #26375

Closed
KoBeWi opened this issue Feb 27, 2019 · 4 comments
Closed

Allow for comparing variables of different types #26375

KoBeWi opened this issue Feb 27, 2019 · 4 comments

Comments

@KoBeWi
Copy link
Member

KoBeWi commented Feb 27, 2019

Godot version:
3.1 beta8

Issue description:
Right now you can't compare variables of different types. To do so, you first need to use typeof of whatever to check whether they are the same type and then compare it. The type-checking step is done anyways, as comparing variables of different types results in an error. So why not, instead of giving error, just return false?

Then we could do
if 1 == "0": (imagine 1 and "0" are variables not hard-coded values)
instead of
if typeof(1) == TYPE_STRING and 1 == "0":
which both result in false, but the former is much cleaner.

@Zylann
Copy link
Contributor

Zylann commented Feb 27, 2019

Me thinking it's about type coercion: In my opinion, that would open a lot of confusing bugs. Being that permissive in type coercion opens the door to many situations appearing to run fine but fail way later, while mismatching types should not have happened in the first place. Strings and numbers just are not the same thing, I would not expect them to be try-parsed just to absolutely fulfill a condition right away. If I expect a string, I parse it. If the variable can sometimes be a string or sometimes a number, I call this a case to take care of explicitely (which also self-documents), or a design flaw.

After reading again, I realise you mention something slightly different: while 1 == "1" should definitely not return true, you want it to return false rather than throwing an error which halts the script... I don't know which is best in that case, although in my use cases, when this happens it's almost always an actual error in my code. If a variable got misassigned to a mismatching type, it's it a lot quicker to find.
Python behaves like your proposal, though.

@KoBeWi
Copy link
Member Author

KoBeWi commented Feb 27, 2019

when this happens it's almost always an actual error in my code. If a variable got misassigned to a mismatching type, it's it a lot quicker to find.

Eh, then just change the error to warning, so I can disable it. (and ofc it should return false)

@Calinou
Copy link
Member

Calinou commented Mar 22, 2019

@KoBeWi You can shorten the type check (while making it faster) using the is keyword, which works with all types since 3.1:

if 1 is String and 1 == "0":
    pass

I think it's fine the way it is, but I'm not opposed to changing it to match Python's behavior.

@KoBeWi
Copy link
Member Author

KoBeWi commented May 25, 2020

This problem was basically gone when I started using types .-.

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