-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
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
For loop with true iterators instead of arrays #3765
Comments
for i in range(1,10) ? |
@nunodonato Custom iterators are actually implemented in Godot already in the Variant class, but they're not documented. You can turn any class into an iterator by overriding Should we change |
@sheepandshepherd |
I believe you should use this instead:
|
the point is, it seems GDScript is trying to stay close to the python syntax (similar discussions have been happening in other issues), so in this case, too, I think it would be better to do it as python does and not reinvent the wheel. for large numbers you can use a while as it seems in python a 'for' is used to iterate over "iteratable" things :) |
+1. Fixing the crash of #3756 is anyway a good thing and @est31 has done a nice job in his PR. A further step would be to prevent using such big sizes for range as it's very inefficient anyway. As you say, if one needs to iterate over 1M elements, better use a while loop. |
I thought it was working with a true iterator, the code was added to Variant to handle this, but seems I forgot to add it.. |
Interesting. Python's range() had this same situation: in Python 2 it created a list, but in Python 3 it was changed to an iterator. In Python 2 the iterator was called The Python 3 way would break code that's intentionally using Edit: made an example range iterator in C++. It could still use improvement (error-checking/constructors?), but it can do everything func example():
for i in RangeIterator.new().set_range(10):
print(i)
for i in RangeIterator.new().set_range(5, 10):
print(i)
for i in RangeIterator.new().set_range(10, -10, -5):
print(i) |
Maybe we can generate an iterator only in some cases? Or make the iterator switch to Array mode whenever you change it... |
That'd be an useful modification. The array allocation should only happen if you add, change, or remove elements. I'm sure we can do better than python here. |
Hmm. I don't think a variable can change its own type, so changing to an Array automatically would be difficult. The RangeIterator class I made now has a |
|
Are the index and index-assignment operators overridable for GDScript? That would allow me to turn the iterator into a pseudo-array. |
@sheepandshepherd theoretically they can be overridden, yes (functions I've added your commit to #3814. The PR is I think the best way to solve this issue. It takes a conservative approach, mimicking Python 2, but being as fast as Python 3 in the common use case of the for loop. |
BTW, there are some other things that might (or... might not) work better with iterators, like |
Reopening as the previous fix has been reverted. |
I think that's closable after @reduz's recent commits... |
What was changed? I think this issue is still worth looking into. |
corner case, use while() instead
…On Thu, Oct 26, 2017 at 10:26 AM, Alexander Taylor ***@***.*** > wrote:
What was changed? I think this issue is still worth looking into.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3765 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AF-Z2516RWjSXeueTquJ6OG3l8KUWv1Uks5swIiggaJpZM4HdeWS>
.
|
Often need a simple classic "for" both "с".
Create an array and a listing of the iterator is not the best option. It's slow and consumes a lot of memory. A realization of the cycle through "while" is not always convenient.
Discuss?
The text was updated successfully, but these errors were encountered: