Question about For loops...

BlitzMax Forums/BlitzMax Beginners Area/Question about For loops...

sswift(Posted 2006) [#1]
I'm pretty sure it is possible to make one's own list type which you can iterate through using For Each, by including a special method in your type.

But is it possible to do the same thing with For X = Y to Z Next? Are there methods which you can put in a type which would allow you to make a list type, and then pass two link pointers as Y and Z, and have Next set X to the next link each time, until it reaches Z? This would be much more convenient than having to do While loops all the time.

If it does work, I wonder if you could make it work properly with Null being passed for both Y and Z. With integers if you pass 0 and 0 then it will loop once, but you don't want it to loop once if the value passed is Null.


Dreamora(Posted 2006) [#2]
No it won't work.

Eachin type only works because it accesses an object enumerator (TMap is a good ressource on how to iterate different things btw), it does not access actual values.

But on the other side ... if you set some borders for the enumerator before the eachin ... it should work thought :-)


sswift(Posted 2006) [#3]
Do you mean a function call before the For loop to set some globals in a custom list type? Or are you talking about some special For Each command format that I am not aware of?

Also, I wonder how Max knows what enumerator to use? Does it use a type with the same name but Emun appended to it like the lists do? I wouldn't have thought the compiler would know those types are related... But I guess it could. Until I see this work, I'm going to suspect it won't work and that the tlist is some special case in the compiler. On the other hand, Mark was talking about how he made lists the way he did cause he wanted functions that work on lists and binary trees and stuff, so it stands to reason he would have made this work with other types. Hm.


Dreamora(Posted 2006) [#4]
Eachin -> method ObjectEnumerator
TMap.Values -> returns a modified enumerator for values that can be "eachin-ed"
TMap.Keys -> returns a modified enumerator for keys ...

And yes I mean a function / method that sets the border before using eachin so the enumerator uses the 2 mentioned objects to create a "sublist" for enumeration.


FlameDuck(Posted 2006) [#5]
Also, I wonder how Max knows what enumerator to use?
The one returned by ObjectEnumerator(). Which is actually an itterator.

Does it use a type with the same name but Emun appended to it like the lists do?
That's not how it works. Look here: Help -> Language -> Collections.