Lua之wrap函数用法示例

wrap和Create差不多,都是去创建一个coroutine,有些区别:

1,wrap不会通过resume去得到第一个返回值(错误信息)
2,在创建完之后,直接调用函数,转到coroutine,而create却要resume才能转到coroutine。
3,wrap不能查看状态。

例子代码:

do  
    function createWrap() 
        return coroutine.wrap(function(x) 
                                    print("Hello", x); 
                                    coroutine.yield(); 
                                    print("continue") 
                                end); 
    end 
 
    coA = createWrap(); --get the function, resum the coroutine 
    coA(3); 
    coA(3); --call the global function, , resum the coroutine 
 
end