lua-users home
lua-l archive

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


Definitely not pretty and probably hard for other programmers to follow.

It could be re-written as:

    function Mutex:do( ... )
        self:lock()
        ( function( self, success, ... )
            self:unlock()
            if not success then error( ..., 2 ) end
            return ...
        end )( self, pcall( ... ) )
    end

I'm not sure that's prettier but it at least avoids the utility function
sitting around separately. On the other hand, I suspect that it will create
a new function closure each time it is called. I'd like to think that the
latter problem could be solved by the compiler noticing that the function
doesn't bind to any local values and hence could be transformed into the
code below, but that probably runs afoul of the ability to change the
environment of Mutex:do.

Mark

on 8/20/04 7:42 PM, Edgar Toernig at froese@gmx.de wrote:

> With the new varargs you can implement it without heap allocations:
> 
> local function do2(self, success, ...)
>       self:unlock()
>       if not success then error(..., 2) end
>       return ...
> end
> 
> function Mutex:do(...)
>       self:lock()
> do2(self, pcall(...))
> end
> 
> Not pretty but reasonable fast.