Lua cannot magically give new arguments to the original arguments. Perhaps they will no longer be on the stack, depending on the optimization. In addition, there is no indication of where the code was when it gave way, so it will no longer be able to see these arguments. For example, if a coroutine is called a function, this new function cannot see the arguments passed to the old one.
coroutine.yield() returns the arguments passed to the resume call, which continues the coroutine, so that the yield call site can process the parameters it wants. This allows the code making the resume to communicate with the specific code that performs the return. yield() passes its arguments as return values โโfrom resume , and resume passes its arguments as return values โโto yield . This sets the path to communication.
You cannot do it otherwise. Of course, not by changing the arguments, which may not be available from the yield site. It is simple, elegant and makes sense.
It was also considered extremely rude to shout out any values. Especially the function is already working. Remember: arguments are only local variables filled with values. The user should not expect the contents of these variables to change unless it changes them themselves. After all, these are local variables. They can only be changed locally; hence the name.
source share