Syntax for generators with resume arguments

I'm not sure we're communicating the same thing here.

In particular, I'm not sure what you mean by "decoupling receiving from yielding". I think we agree that yielding has to be coupled with receiving on some level; as in, the function has to receive a new value every time it yields. The problem is how we handle the first and/or last yield.

Anyway, the first JS example you give seems close to the semantics I think would be ideal:

  • From the "inside", the generator can always expect function.sent to have a usable value (which means that, wherever you are in the generator body, resume has already been called at least once). No special case.
  • From the "outside", the generator can always be used like a function from the get go: pass arguments, get return value.

How exactly these semantics are achieved is secondary, as long as the two conditions above are respected. RFC PR #2781 achieves these semantics by having the closure arguments also be the resume arguments, but that's not the only approach.

I think an important feature with the AI example is that the code doesn't care how many times resume has been called; and in fact, "is this the first yield" isn't even something the generator body knows.

If there was a player_pos_init and terrain_init values that were passed as functions argument, and were different from the regular player_pos and terrain values that the generator uses, then the generator would need to do some work to convert from one to the other. This work would be pure boilerplate, with no semantic value.

In the example code, the generator's entire body is a loop, with no setup or teardown code. If the AI finds a condition that resets it to its default behavior, all the code has to do is continue 'main_loop and it is guaranteed to behave as if the generator had been called for the first time.

I think that's desirable. Most generators ideal representation is (eg your map example) is a loop, with nothing before or after the loop body. That's only possible if the two properties mentioned above are respected.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.