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

Add a python-like enumerate option for iterators #7651

Open
MathiasBaumgartinger opened this issue Sep 13, 2023 · 6 comments
Open

Add a python-like enumerate option for iterators #7651

MathiasBaumgartinger opened this issue Sep 13, 2023 · 6 comments

Comments

@MathiasBaumgartinger
Copy link

Describe the project you are working on

irrelevant

Describe the problem or limitation you are having in your project

Especially for for each loops it is quite cumbersome to always index the container

var array: Array

for i in range(array.size()):
   ... array[i] ... 

or to always add an additional variable:

var array: Array
var count := 0
for element in array:
  ...
  count += 1

However an enumerate does have numerous additional use cases.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

for each loops with index produce worse readable and/or additional unnecessary code lines, which could be easily avoided.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Just like in python a:

for i, element in enumerate(array):
  ...

would do the trick.

If this enhancement will not be used often, can it be worked around with a few lines of script?

I think i can confidently assume this would be used a lot.

Is there a reason why this should be core and not an add-on in the asset library?

Needs to be done at core level (to my knowledge it would even require a slight rewrite of "iteration" code base).

@Calinou
Copy link
Member

Calinou commented Sep 13, 2023

for i in range(array.size()):

Can be shortened to:

for i in array.size():

This is also slightly faster in terms of run-time performance.

@Mickeon
Copy link

Mickeon commented Sep 13, 2023

I am to assume the proposed implementation (with the Array method) would require it returning tuples, which Godot doesn't have, and I'm not sure it will anytime soon.

One other way to go about though, would be to be able to define two variables in the for loop, separated by a comma. One is inferred to be the index, and the other is the value. There was something similar suggested for Dictionaries (which would heavily benefit from this), but I do not have the proposal at hand.

@Zireael07
Copy link

Godot can return lists though, and I often return lists of [x.y] :P

@nlupugla
Copy link

I like the idea in principle, but adding new global scope methods like this technically breaks compatibility. That said, changing the syntax to array.enumerate() would solve this issue.

The bigger problem is that GDScript does not yet support multiple assignment with a line like for i, element in array.enumerate.

@MathiasBaumgartinger
Copy link
Author

I am to assume the proposed implementation (with the Array method) would require it returning tuples, which Godot doesn't have, and I'm not sure it will anytime soon.

One other way to go about though, would be to be able to define two variables in the for loop, separated by a comma. One is inferred to be the index, and the other is the value. There was something similar suggested for Dictionaries (which would heavily benefit from this), but I do not have the proposal at hand.

I suppose you are talking about issue #1965

@PinkSerenity
Copy link

I am to assume the proposed implementation (with the Array method) would require it returning tuples, which Godot doesn't have, and I'm not sure it will anytime soon.

#2135 is about array unpacking, which would work as well.

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

7 participants