iterator.repeat
improvements and string.repeat
#356
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR prevents a case where
iterator.repeat
could be easily misused.For example:
It would be reasonable to expect a result here of
[[1, 2], [1, 2], [1, 2]]
, but instead it causes an infinite loop.iterator
module when a matching item can't be found inlist
.[1, 2]
is passed toiterator.repeat
as the instance for the call.iterator.repeat
is intended to be used as a standalone function and ignores the instance.3
is then treated as the value to repeat, without a count, producing an endless iterator.The solution here is to have
iterator.repeat
check that it's being used as a standalone function.iterator.generate
is less prone to misuse, but is now similarly restricted. The check is a bit hacky, it checks that the instance is eithernull
or theiterator
module, which means thatrepeat
won't work if it's added to another module and then called there. A more flexible solution would involve reworking the module system, which isn't a problem for today.The PR also adds a specialized version of
repeat
for strings, which seems useful enough to treat as a special case, e.g.Additionally, checking for standalone use of
repeat
exposed a flaw where temporary values were left around and ending up in the instance register for standalone calls, so some work has gone into improving the use of temporary registers in lookup chains, and in ensuring that the instance register is cleared when necessary.