lua-users home
lua-l archive

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


on 3/27/06 9:38 AM, Mark Hamburg at mhamburg@adobe.com wrote:

>   class "Book" ( ActiveRecord.Base ) do
>       belongs_to( 'publisher' )
>       has_and_belongs_to_many( 'authors' )
>   end

Again, I'll note that this doesn't work because of the change in semantics
for do, but something like this also opens up constructs like:

    lock_mutex( mutex ) do
        --- do stuff with the mutex locked
    end

Which could be written as (untested):

    local function pcall2call( success, ... )
        if success then
            return ...
        else
            local errmsg = ...
            error( errmsg, 0 )
        end
    end

    local function finish_lock_mutex( mutex, success, ... )
        mutex:unlock()
        return pcall2call( success, ... )
    end

    function lock_mutex( mutex )
        return function( block )
            mutex:lock()
            return finish_lock_mutex( mutex, pcall( block ) )
        end
    end

The downside to this is that it generates two closures instead of the one
needed if we use the form:

    lock_mutex( mutex, function()
        -- do stuff with the locked mutex
    end )

Mark