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

set() or object assignment looks for more specific type than returned by typeof() #93067

Closed
za3k opened this issue Jun 12, 2024 · 2 comments
Closed

Comments

@za3k
Copy link

za3k commented Jun 12, 2024

Tested versions

Found in Godot 4.2.2.

System information

Godot v4.2.2.stable unknown - Arch Linux #1 SMP PREEMPT_DYNAMIC Fri, 31 May 2024 15:14:45 +0000 - Tty - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4060 Ti (nvidia; 550.78) - Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz (24 Threads)

Issue description

I'm filing a usability bug. I think everything below is working as designed, I'm just running into some problems with parts of the design being inconsistent, and it making Godot less usable in practice.


When using: self[k] = v, it appears the type has to match exactly. Not just Array, but Array[int]. That's fine.

However, there doesn't seem to be an easy way to actually do the conversion, because typeof() returns only the Variant.Type Array, and type_convert() also looks only at Variant.Type. So if you start with untyped data, which you perhaps know the type of, I'm not sure of a way to add typing information. This is a usability bug.

Below is a short reproduction case of the issue. (For this sample code, it's also relevant that you can't annotate the types of individual dictionary values.)

Note that for the related built-in function set(), the same failure happens. Instead of a user-visible error, typed Array values are silently skipped (when assigned value is of type Array), which is the original bug I dug into.

Ideally, I would like the final reproduction code to work, whether because the []= operator becomes more permissive about typed arrays, or the convert() operator starts accepting more specific types.

self["cannot_be_set"] = type_convert(constants["cannot_be_set"], typeof(self["cannot_be_set"]))

Also relevant:

Steps to reproduce

extends Node2D

var can_be_set : int
var cannot_be_set : Array[int]

var constants = {
	"can_be_set": 1,
	"cannot_be_set": [2, 3, 4],
}

func _ready():
	# Works
	self["can_be_set"] = constants["can_be_set"]
	
	# Fails
	self["cannot_be_set"] = constants["cannot_be_set"]
	
	# Fails
	var t1 := typeof(cannot_be_set)
	self["cannot_be_set"] = type_convert(constants["cannot_be_set"], t1)

	# Fails
	var t2 := typeof(self["cannot_be_set"])
	self["cannot_be_set"] = type_convert(constants["cannot_be_set"], t2)

Minimal reproduction project (MRP)

bug-repro.zip same code as above

@dalexeev
Copy link
Member

I'm filing a usability bug. I think everything below is working as designed, I'm just running into some problems with parts of the design being inconsistent, and it making Godot less usable in practice.

This tracker is intended for engine or documentation bugs. If you want to propose a new feature or change to functionality that works as designed, please use godot-proposals instead.

You raised several questions at once:

  1. Type casting when assigning a typed variable, passing a typed argument to a function, and so on. Some types are implicitly convertible (like int and float), some require an explicit cast/conversion.
  2. Array literals ([]) are untyped by default, but in a typed context they are implicitly converted to typed arrays. There is no convenient typed array constructor, however you can use [] as Array[int] or Array([], TYPE_INT, &"", null).
  3. Lack of a unified type system; you cannot explicitly cast a value to a type regardless of its kind (built-in types, native and custom classes, typed arrays).

However, I think all these questions are already known, documented or being discussed:

@za3k
Copy link
Author

za3k commented Jun 12, 2024

This tracker is intended for engine or documentation bugs.

That is fair, we can close this if it doesn't feel "bug-like" enough.

If you want to propose a new feature or change to functionality that works as designed, please use godot-proposals instead.

I'm not sure I'm knowledgeable enough to make a proposal for a change myself. That might have to wait a year. (For example, I wasn't even aware of the as keyword--I'm a total newbie.)

If there was a place to add use-cases in the context of discussion I would do that, but there's not. I do think there are some minor additions here. This is the first request I've noticed for "convert to the same as" or of wanting a function to return the GDScript type of an array. Most of the other requests are using hardcoded types. Other than that, I would agree that the individual components have been discussed.

I don't feel I'm educated enough to request a specific right solution to this problem, and I agree this isn't a pressing specific bug, so I'll close it.

@za3k za3k closed this as completed Jun 12, 2024
@github-project-automation github-project-automation bot moved this from For team assessment to Done in GDScript Issue Triage Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

3 participants