[fpc-pascal] For ..in GetEnumerator Allocation
Michael Van Canneyt
michael at freepascal.org
Wed Oct 4 11:26:50 CEST 2017
On Wed, 4 Oct 2017, Ryan Joseph wrote:
> As I understand the for..in loop GetEnumerator method is expected to create a new object each time it’s called
> and FPC destroys it later when the loos is finished. Can I retain the enumerator and just reset it in-between calls?
> I’d like to remove all these alloc/deallocs so I can use for..in more efficiently in tight loops.
You can do so by overriding the newinstance and it's sister method of
your enumerator class.
As an alternative you can create an object enumeator.
It's simply allocated on the stack, and you can reset it in the enumerator
See the example in http://wiki.freepascal.org/for-in_loop
section: Using any identifiers instead of builtin MoveNext and Current
More information about the fpc-pascal