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

Typed arrays aren't working with += #72948

Closed
EamonnMR opened this issue Feb 9, 2023 · 3 comments · Fixed by #73540
Closed

Typed arrays aren't working with += #72948

EamonnMR opened this issue Feb 9, 2023 · 3 comments · Fixed by #73540

Comments

@EamonnMR
Copy link

EamonnMR commented Feb 9, 2023

Godot version

v4.0.rc1.official [8843d9a]

System information

Lubuntu

Issue description

You can't use + or += to mutate typed arrays.

It seems to be not correctly getting the type information from the second operand.

An erudite poster on the discord puts it differently:

it's more like operator + doesn't retain Array typing
the syntax for Array Concatenation via operators, but the idea gets across i think

The error you see is:
Trying to assign an array of type "Array" to a variable of type "Array[int]".

Steps to reproduce

	var Alice: Array[int] = [1,2,3]
	var Bob: Array[int] = [4,5,6]
	Alice += Bob

This will crash with the above error message.

Minimal reproduction project

plus_syntax_broken_demo_project.zip

@Haledire
Copy link

Haledire commented Feb 9, 2023

Specifically - the operator + as the docs describe returns a Variant Array
image

when you do (in this example) Alice + Bob, the returned array loses the typing - going from Array[int] to just Array

append_array() works to concatenate the same typed arrays, but the operator itself can't seem to maintain the typing

Alice = Alice + Bob   # fails
Alice += Bob          # fails
Alice.append_array(Bob)  #succeeds

@esarver
Copy link

esarver commented Feb 10, 2023

I see the same issue when initializing arrays like:

var membervar: Array[ClassDefinedElseWhere] = Array()

It seems like this is a more general issue.

@YuriSizov
Copy link
Contributor

YuriSizov commented Feb 10, 2023

@esarver This is not the same issue. You are trying to assign an untyped array to a typed property, which is not possible due to ambiguous behavior. You need to use the assign method of the array if you want to convert your value to the typed array.

If you only want to initialize your property, an array literal should work, [].

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

Successfully merging a pull request may close this issue.

6 participants