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

GDScript 2.0: Can't use not operator (!) with String, Dictionary, or Array unless const #58579

Closed
Error7Studios opened this issue Feb 27, 2022 · 5 comments

Comments

@Error7Studios
Copy link

Error7Studios commented Feb 27, 2022

Godot version

4.0 alpha 3

System information

Windows 10

Issue description

In Godot 4, empty String, Array, and Dictionary values convert to true booleans, unlike Godot 3.
Is this intended?

Also, you can no longer use not/! in front of said values, unless they're saved into a const.

Steps to reproduce

extends Node

const VALUE = {}

func _ready():
	var val = VALUE				# Error
#	var val := VALUE			# Error (static type)
#	const val = VALUE			# OK
#	const val := VALUE			# OK (static type)
	
	if !val:
		print(val, " is falsy")
	else:
		print(val, " is truthy")

Minimal reproduction project

Godot4 NOT Operator.zip

@Calinou Calinou added this to the 4.0 milestone Feb 27, 2022
@Calinou Calinou changed the title Can't use not operator (!) with String, Dictionary, or Array unless const GDScript 2.0: Can't use not operator (!) with String, Dictionary, or Array unless const Feb 27, 2022
@dalexeev
Copy link
Member

#	const val = VALUE			# OK
#	const val := VALUE			# OK (static type)

In RC 1, all 4 options give the same error (Invalid operand of type "Dictionary" for unary operator "not".).

@YuriSizov
Copy link
Contributor

YuriSizov commented Feb 11, 2023

Also all 3 types have a method to explicitly test for emptiness that should be used. I wouldn't call this a regression, it's more of a change for consistency.

@decree
Copy link

decree commented Feb 15, 2023

So this is intended behaviour and not a bug?

As i'm porting my old Godot 3.5 project to Godot 4, i have tons of if not <string> or if not <dictionary> checks which now result to an error. I think the old 3.5 way to do a simple if not x was an effective and clean way to check things, because it worked with any variable without or with typing.

@FilipLundby
Copy link

FilipLundby commented Feb 26, 2023

If this is about consistency, I'm curious why it's allow to test on strings as long as you do not use ! and not. For instance in C# if ("hello world") ... is not allowed, while in JavaScript you can do both if ("hello world") ... and if (!"hello world") ....

# Prints "A"
if "Go-dot, or go home":
	print("A")
else:
	print("B")

# Prints "B"
if "":
	print("A")
else:
	print("B")

# Invalid operand of type "String" for unary operator "not".
if !"": 
	print("A")
else:
	print("B")

To me this seems inconsistent.

Godot 4.0 RC 5

@Lippanon
Copy link

I also agree it seems inconsistent to allow:

var example_dict :Dictionary = {}
if example_dict:
	pass

But not allow not example_dict. I use the example above to check the result of intersect_ray frequently and finding out I can't check its negation like that made me question if the example above can result in undefined behavior.

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

8 participants