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

Filled array with array iterates "wrongly". #91501

Closed
PeterMarques opened this issue May 3, 2024 · 7 comments
Closed

Filled array with array iterates "wrongly". #91501

PeterMarques opened this issue May 3, 2024 · 7 comments

Comments

@PeterMarques
Copy link

Tested versions

3.5.3

System information

editor

Issue description

When running:

extends Node

func _ready():
	var a = []
	
	a.resize(3)
	a.fill([])
	
	for each in a.size():
		a[each].append(randi())
		
	print(a)
	
	var b = []
	b.resize(3)
	
	for each in b.size():
		b[each] = []
		b[each].append(randi())
		
	print(b)

you get:

[[3161026589, 2668139190, 4134715227], [3161026589, 2668139190, 4134715227], [3161026589, 2668139190, 4134715227]]
[[4204663500], [1099556929], [3154986942]]

It seens that both outputs must be like the second one.

The first case is iterating again inside the branches, and copying the values.

Or i am missing something? i tried using fill([].duplicate()) but same behaviour.

Steps to reproduce

run the code

Minimal reproduction project (MRP)

Bug Array.zip

@AThousandShips

This comment was marked as outdated.

@dalexeev
Copy link
Member

dalexeev commented May 3, 2024

a.fill([])

Arrays are passed by reference, you fill the outer array with a reference to the same array. This is already documented:

Note: If value is of a reference type (Object-derived, Array, Dictionary, etc.) then the array is filled with the references to the same object, i.e. no duplicates are created.

We could solve this by adding optional parameters duplicate = false and deep = false.

@PeterMarques
Copy link
Author

PeterMarques commented May 4, 2024

a.fill([])

We could solve this by adding optional parameters duplicate = false and deep = false.

and

[...] has been solved in 4.x I][...]

changing to a.fill([].duplicate(false)) still produces the same output, even on 4.3dev5

@dalexeev
Copy link
Member

dalexeev commented May 4, 2024

changing to a.fill([].duplicate(false)) still produces the same output, even on 4.3dev5

This shouldn't change anything. You create the array once, make a duplicate once, and pass the duplicate to fill(), which fills the entire outer array with the same reference, as the documentation says. To create N different arrays you need to use a loop.

In other words, the expression [].duplicate(false) is evaluated only once. You pass to fill() method the expression value, not the expression itself.

@AThousandShips
Copy link
Member

(note the outdated hidden comment, I got this confused with another bug related to shared literals)

@PeterMarques
Copy link
Author

So, the patterns are NOT and SHOULD NOT be equivalent?

Ok, so there is NO WAY to fill a array with a blank arrays using fill() and one need to use the iterator to alocate/or push the blank non referenced arrays inside it, and this is the only way to do that?

If so, ok, no more to discuss, but i, nonetheless, find it odd.

Will just stick to iterate it fully and forget about the fill, and maybe even the resize, on that use case.

Thanks.

@dalexeev
Copy link
Member

dalexeev commented May 8, 2024

So, the patterns are NOT and SHOULD NOT be equivalent?

Yes, they shouldn't.

We could solve this by adding optional parameters duplicate = false and deep = false.

I think this should be opened as a proposal instead, if you're interested. However, the current behavior appears to be correct as is: fill() is intended to fill an array with identical elements. Arrays that are value-equal but not reference-value probably cannot be considered the same (see also is_same()).

Given the above, let's close this issue, since the behavior is not a bug and is correctly documented. Thanks for the contribution nonetheless!

@dalexeev dalexeev closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2024
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

3 participants