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
<> 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:

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"))

steve d.