YSI-Includes icon indicating copy to clipboard operation
YSI-Includes copied to clipboard

[y_iterate] New/alternative syntax for tagged iterators

Open kristoisberg opened this issue 5 years ago • 4 comments

The current syntax for tagged iterators is definitely not the best possible: https://github.com/pawn-lang/YSI-Includes/blob/5.x/YSI_Data/y_foreach/y_foreach_tests.inc#L1018

The new, suggested syntax which Y_Less agreed with: Iterator:iter1<Group:60>;

kristoisberg avatar Aug 30 '18 19:08 kristoisberg

Update: The current syntax doesn't even seem to work properly...

static Iterator:Light:Lights<MAX_LIGHTS>;
C:\Users\Kristo Isberg\Documents\actually-good-lights\actually-good-lights.inc:38 (error) symbol already defined: "Lights"
C:\Users\Kristo Isberg\Documents\actually-good-lights\actually-good-lights.inc:44 (error) undefined symbol "Iterator@Lights"
C:\Users\Kristo Isberg\Documents\actually-good-lights\actually-good-lights.inc:44 (error) undefined symbol "Iterator@Lights"
C:\Users\Kristo Isberg\Documents\actually-good-lights\actually-good-lights.inc:44 (error) undefined symbol "Iter_Single@Lights"
C:\Users\Kristo Isberg\Documents\actually-good-lights\actually-good-lights.inc:44 (fatal) too many error messages on one line

Removing the tag from the iterator fixes it. Preprocessor output:

static Iterator@Light:Lights[_:F@c:F@d:((100)+1)]={0,1,...},Iter_Single@Light:Lights ;

kristoisberg avatar Aug 30 '18 20:08 kristoisberg

Just a note: the current syntax is both bad and quite hard-coded. It only works for pre-defined tags:

https://github.com/pawn-lang/YSI-Includes/blob/b79f10013c2a2d3d97010f00a7d467a75bb40bd6/YSI_Data/y_foreach/y_foreach_macros.inc#L696-L711

That's why your test sadly didn't work @kristoisberg .

Y-Less avatar Oct 22 '18 16:10 Y-Less

I've got it working for normal iterators now. With some conditions:

  1. Tagged special iterators have not been tested.

  2. A slightly odd error message in some cases:

new
	Iterator:iter1<Group:60>;
Iter_Add(iter1, 44);

Gives:

tag mismatch: expected tag "none@iter", but found "Group"

The tag isn't quite _: because I couldn't get it to work using that internally. I considered using the tag none so the warning was very similar to the default:

expected tag none ("_"), but found "Group"

And:

expected tag "none", but found "Group"

However, I was not keen on defining none: as a generic macro. Maybe something like iter_none, iter_tag, none_iter, or something else would be clearer that it is supposed to be no tag, but without a too generic name (I've considered all of those, but wouldn't mind some feedback).

Also, it should be expected tag "Group", but found "none" - the order is currently wrong.

  1. Some internal functions totally remove tags:
Iter_TrueArray(Groups);
Iter_First(Groups);
Iter_Last(Groups);

Those all return untagged arrays or values. Mostly because those are the only functions that take JUST an array.

Y-Less avatar Oct 22 '18 21:10 Y-Less

Right, I've subtly improved the implementation further. I fixed adding tagged variables, i.e. this didn't work before:

new Group:a = Group:55;
Iter_Add(TaggedIter, a);

Well, it worked but gave a warning. Now it doesn't. I also realised another limitation:

  1. You must be completely explicit with the tag in the declaration. This won't work:
#define MAX_THINGS (Thing:50)
new Iterator:Things<MAX_THINGS>;

This will:

#define MAX_THINGS (Thing:50)
new Iterator:Things<Thing:MAX_THINGS>;

Y-Less avatar Oct 23 '18 13:10 Y-Less