lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Sep 11, 2011 at 5:40 AM, Daniel Aquino
<mr.danielaquino@gmail.com> wrote:
> With pcall couldn't you implement the defer functions in Go ?
> This is pseudo code somewhere between Javascript and Lua...

Have a look at how it's done in LuaSocket:

http://w3.impa.br/~diego/software/luasocket/socket.html

look for the example under socket.newtry.

foo = socket.protect(function()
    -- connect somewhere
    local c = socket.try(socket.connect("somewhere", 42))
    -- create a try function that closes 'c' on error
    local try = socket.newtry(function() c:close() end)
    -- do everything reassured c will be closed
    try(c:send("hello there?\r\n"))
    local answer = try(c:receive())
    ...
    try(c:send("good bye\r\n"))
    c:close()
end)

steve d.