enumerable class w/o exposing implementation lazarus / 61-Beginners ---------------------------------------------------------------------- Kays · 2016-03-30 23:49 Hi, how do I make my class enumerable for a for..in-loop without exposing anyhow the enumeration implementation by putting the methods getEnumerator(), moveNext() and property current into the public section? The compiler shall be able to enumerate my class just in for..in- loops, but the user of the class shall not be able to manipulate my enumerable object, since other objects rely on its state. ---------------------------------------------------------------------- Thaddy · 2016-03-31 07:02 Well, make the getenumeator call (strict) private.... See here for an example that also works in FPC delphi mode: http://stackoverflow.com/questions/6389357/how-to-properly-make-an- interface-support-iteration ---------------------------------------------------------------------- Kays · 2016-03-31 16:01 Quote from: Thaddy on March 31, 2016, 09:02:43 amWell, make the getenumeator call (strict) private.... See here for an example that also works in FPC delphi mode: http://stackoverflow.com/questions/6389357/how-to-properly-make-an- interface-support-iteration Then I get at the for..in-line: Free Pascal Compiler version 3.0.0 [2015/12/05] for x86_64 Copyright (c) 1993-2015 by Florian Klaempfl and others Target OS: Linux for x86-64 Compiling myClassTest.pas myClassTest.pas(42,9) Error: Cannot find an enumerator for the type "TListEnt" myClassTest.pas(100) Fatal: There were 1 errors compiling module, stopping Fatal: Compilation aborted Error: /usr/bin/ppcx64 returned an error exitcode ---------------------------------------------------------------------- Thaddy · 2016-04-08 04:57 Try this: http://blog.marcocantu.com/blog/enumerators_nested_types.html ---------------------------------------------------------------------- Graeme · 2016-04-12 10:55 Take a look at the article (and accompanied source code) I wrote on the Iterator Design Pattern. [http://geldenhuys.co.uk/articles/] It is a lot more flexible than the FPC built-in enumerator solution which can only go forward, can't support regex, can't be reset etc. The accompanied source code includes enumerators for most used list classes of FPC. It is very easy to extend. You can even extended it to iterate say code points of a UTF-8 or UTF-16 encoded string (including support for surrogate pairs) etc. Sample usage: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  itr: ITBStringIterator;begin  ...  itr := gIteratorFactory.StringIterator(sl);  while itr.HasNext do    writeln(itr.Next);  ...end;  Now lets throw some regex in the mix... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fitr := gIteratorFactory.FilteredStringIterator(MyStringsCollection, 'foob.*r');while fitr.HasNext do  DoSomethingWithItem(fitr.Next);  Hope you find this useful.