lua-users home
lua-l archive

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


On 11/1/07, steve donovan <steve.j.donovan@gmail.com> wrote:
> This isn't very pretty, but it works:
> in particular, this method only allows one value to be returned from the
> expression.

Pardon me replying to myself! Pass #2

-- macro-defs.lua

macro('_quote',{'expr'},@function() return (expr) end@)

function _pcall_assert(callable,msg)
	local status,a1,a2,a3,a4 = pcall(callable)
	if not status then error(msg,2) end
	return a1,a2,a3,a4
end

macro('fred',{'x'},@
_pcall_assert(_quote(some_other_function(x)),"fred failed") @)

function some_other_function(a)
	return a.x
end

-- test-fred.lua
a = {x = 2}
print(fred(a))
print(fred(nil))

$ lua -lmacro -lmacro-defs test-fred.lua
2       nil     nil     nil
lua: test-fred.lua:3: fred failed
stack traceback:
        [C]: in function 'error'
        .\macro-defs.lua:66: in function '_pcall_assert'
        test-fred.lua:3: in main chunk
        [C]: ?

Here at least we don't have to insert the body of _pcall_assert(), and
we can return up to four return values. This feels like a job for
select(), but I don't how to use it in this context! Otherwise it's a
nasty {...} construct, which is inefficient and doesn't handle nils.

steve d.