Skip to content

Conversation

@mnemnion
Copy link

This changes the library so that string tags are auto-converted into a private table, and so that the return coroutine table is cached by tag.

The combination means that a user can get a consistent set of nested coroutine tools by string, from any module:

local coro = coromake 'scheduler'

Will return the same coro for any unique string, without causing trouble if that string is yielded by this or another yield function.

The alternative in the original code would involve either making global or passing around, some common table used only for this purpose, or choosing some metatable or function deemed unlikely to be used, and hoping we don't forget.

The original code also allows a string argument, but then will exhibit strange behavior if that string is yielded, ever, and that's the sort of bug no one wants to have. The semantics are unchanged if the argument isn't a string, and I would argue that what this patch does with strings is safer and more convenient.

This patch makes two changes: it caches the return values by tag, and auto-converts a string tag into a private table.

The combination of these means that any module can call something like 

```lua
local coro = coromake 'scheduler'
```
And will obtain the same nested coroutines table, without running into a problem with using `yield 'scheduler'` somewhere as a literal value.

Which is visible in the example, but the silent bug is when the string slips into some process or collection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant