Research on data return between Resume-yield of collaborative programs in Lua _lua

Source: Internet
Author: User
Tags lua

It's simple to introduce a few this time, but be sure to return the value rule carefully.

1.resume of parameters

The resume function can continue to pass other parameters besides the first parameter, the following code:

Copy Code code as follows:

Local CO = coroutine.create (function (name)
print (name);
End);
Coroutine.resume (CO, "resume Param");

Resume the second parameter is "Resume Parame", which is passed to the function of the synergistic program.
The output results are as follows:

Copy Code code as follows:

[Lua-print] Resume param

It's easy, right, remember this rule, then don't mess up.

The second return value of the 2.resume function

Remember the two return values of the resume function? One represents the correct execution of the cooperative program, and one represents the error message.

Well, if the collaborative program executes correctly, the return value of the error message is naturally nil.

Then there is the rule that the parameters of the yield function can be passed to the second return value of resume.

The following code:

Copy Code code as follows:

Local CO = coroutine.create (function (name)
print (name);
Coroutine.yield ("yield param");
End);
Local result, MSG = Coroutine.resume (CO, "resume Param");
Print ("msg:"..) msg);

The output results are as follows:
Copy Code code as follows:

[Lua-print] Resume param
[Lua-print] Msg:yield param

This time we just added a yield call, while the yield function we passed a parameter in.
This function will be the second return value of resume, provided that the first return value of the Resume function is true.

What do you think? Is it starting to get a little messy?
Never mind, the next more confusing.

The return value of 3.yield

This is the return value of the yield, take a look at the following code:

Copy Code code as follows:

Local CO = coroutine.create (function (name)
For i = 1, 2, 1 do
print (name);
Print ("Co:" ...). Coroutine.yield ("yield param"));
End
End);
For i = 1, 2, 1 do
Print ("=========" ...). "Secondary Execution:")
Local result, MSG = Coroutine.resume (CO, "resume Param");
Print ("msg:"..) msg);
End

The collaboration program executes A for loop, and we also call the two resume function, and the output results are as follows:

Copy Code code as follows:

[Lua-print] ========= 1th time execution:
[Lua-print] Resume param
[Lua-print] Msg:yield param
[Lua-print] ========= 2nd time Execution:
[Lua-print] Co:resume param
[Lua-print] Resume param
[Lua-print] Msg:yield param

For the first time, the collaboration program is suspended for the first time, so the yield return is not available until the second resume is invoked.

So, the second call to resume, the first time you get the return value of the last yield, this return value is the resume of the second parameter.

Yes, resume's second return value is the yield argument, and yield's return value is the second parameter of resume.

More simply, the return value of the resume is the yield parameter, and the yield return value is the resume parameter.
At the same time, the second parameter of resume can also be passed to the function of the cooperative program.

What do you think? A little bit of confusion, huh?

It doesn't matter, the more confusing situation will happen again, hehe. (Jor: Oh your head, I left)

4. The return value of the main function at the end of the cooperative program

This last case, that is the function return value of the collaborative program, yes, it can also have the return value.
First look at the code:

Copy Code code as follows:

Local CO = coroutine.create (function (name)
For i = 1, 2, 1 do
print (name);
Print ("Co:" ...). Coroutine.yield ("yield param"));
End
Return "Collaborative program function end LA!" "
End);
For i = 1, 3, 1 do
Print ("=========" ...). "Secondary Execution:")
Local result, MSG = Coroutine.resume (CO, "resume Param");
Print ("msg:"..) msg);
End

I added a return value at the end of the collaboration program function, just a string.

And resume's call I increased to 3 times, this is because the collaborative program's for Loop will execute two times, namely will call yield two times.

Therefore, the second yield can be returned when the resume function needs to be executed for the third time.

The output results are as follows:

Copy Code code as follows:

[Lua-print] ========= 1th time execution:
[Lua-print] Resume param
[Lua-print] Msg:yield param
[Lua-print] ========= 2nd time Execution:
[Lua-print] Co:resume param
[Lua-print] Resume param
[Lua-print] Msg:yield param
[Lua-print] ========= 3rd time execution:
[Lua-print] Co:resume param
[Lua-print] msg: End of the collaborative program function LA!

The previous two results have not changed, the third time a little special.

The third execution of resume, the first to get the second yield return, output "Co:resume param."

Note that this is not going to continue with print (name), which means that the entire for loop of the Synergistic program function will not be executed.

This is only the second time the yield function returns the result, which can not be misunderstood.

Finally, the collaborative program function returns a string that becomes the second return value of the Resume function.
Yes, the second return value of resume is no longer a yield parameter when the collaboration program executes.

5. End

Well, this is the place I had to study carefully after I accidentally swept my eyes today.

Although not yet understand the actual application of these rules, but this must be recorded, because I will soon chaos, and then have to look back at this article ~

Well, it's already 9 o'clock ... The beautiful Friday evening t_t

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.