-
-
Notifications
You must be signed in to change notification settings - Fork 21k
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 - Static typed String/StringName with type checking will result an error #66015
Comments
The "incorrect" lines for the first two blocks are marked as unsafe (line number in gray) godot/modules/gdscript/gdscript_analyzer.cpp Lines 2178 to 2179 in 24115be
The problem for me is the assignment lines, they are not marked as unsafe and don't throw any error.
works while an error is thrown for "istr3 is String" test |
My bad, StringName<->String are valid for assignments godot/core/variant/variant.cpp Lines 578 to 583 in 24115be
|
After more thoughts about that, everything seems normal. So I think it just that unsafe lines are not very obvious (not sure if there's documentation about what the color means). |
Sorry for the late reply. Thanks for the strong matters addition. var untyped_imm = StringName("SN")
var untyped_mut = String("N")
print(untyped_imm is StringName) # OK
print(untyped_imm is String) # OK
print(untyped_mut is StringName) # OK
print(untyped_mut is String) # OK
var typed_imm: StringName
var typed_mut: String
typed_imm = get_name()
typed_mut = typed_imm
# Below 2 lines are worked perfectly.
# And typed_imm and typed_mut appears to be cast correctly.
print(typed_imm, typeof(typed_imm), "=", TYPE_STRING_NAME)
print(typed_mut, typeof(typed_mut), "=", TYPE_STRING)
# These four lines should issue a warning, not an error.
print(typed_imm is StringName) # No error/warning.
#print(typed_imm is String) # Yea, error thrown at edit time.
#print(typed_mut is StringName) # Yup, same as above.
print(typed_mut is String) # No error/warning.
I mostly agree, but the transparent casting of String<>StringName is very helpful, GDScript Reference: Keywords
I thought it would be more appropriate to refer to the last four lines as a warning rather than an error or nothing happening. |
Maybe it's their name that is confusing but
is "OK" but still returns
it's not a cast but a conversion/copy of the value from one type to the other |
Yup, I am aware of that (cast/convert). var typed_imm: StringName
var typed_mut: String
typed_imm = get_name()
typed_mut = typed_imm
print(typed_imm is StringName)
print(typed_imm is String) # Error at beta1&2, but it should be a warning or correct
print(typed_mut is StringName) # Same error at beta1&2
print(typed_mut is String) I modified the text and the code above a bit because it was not clear. |
This is not a bug, it's intended. A variable of type If you are considering the assignment, it will convert the value to the variable type when that is set, so it still is correct. If anything, this could be change to just be a warning (although I would probably make it an error by default). |
@vnen Thanks for ur reply. var typed_imm: StringName = ""
var typed_mut: String = ""
print(typed_imm is StringName) # always true
print(typed_mut is String) # same |
Yes, the reverse case is the same (for builtin types at least), but it hasn't been implemented. I do think those might be better as warnings, even if they are set to error by default. Won't be done for 4.0 release though, since that is too close. |
As discussed in the GDScript team meeting (see #75170 (comment)), However, these types are compatible with each other, meaning you can pass a So you can use |
Godot version
v4.0.beta1.official [20d6672]
System information
It's not system specific error.
Issue description
If it is cast implicitly, it would work in all cases, but it seems that in some cases the conversion between String and StringName does not match the actual type.
I think the type check is not working properly. By the way, casting by
as
worked well.Steps to reproduce
StringName
value to statically typedString
varor
String
value to statically typedStringName
varis
Expression is of type blabla so it can't be of type blabla
Minimal reproduction project
The text was updated successfully, but these errors were encountered: