[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua is not skin deep
- From: "steve donovan" <steve.j.donovan@...>
- Date: Thu, 1 Nov 2007 08:41:23 +0200
On 10/31/07, Asko Kauppi <askok@dnainternet.net> wrote:
> Let's say we have these "skins" (might be a very bad example):
>
> #... -> (select('#',...))
> ...[exp] -> (select(exp,...))
A generally tough problem. One of the less beautiful things about
generic template trickery in C++ is that the error messages reflect
the complexity of the implementation, which makes it very hard to hide
implementation details from the poor user.
This isn't very pretty, but it works:
function _call(f) return f() end
macro('pcall_assert', {'expr','msg'},
@ _call(function()
local result
local res,err = pcall(function()
result = expr
end)
if not res then error(msg) end
return result
end) @
)
macro('fred',{'x'},@ pcall_assert(some_other_function(x),"fred failed") @)
function some_other_function(a)
return a.x
end
Testing it:
$ lua -lmacro -lmacro-defs -i
Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
> a = {x = 2}
> = fred(a)
2
> = fred(nil)
stdin:1: fred failed
stack traceback:
[C]: in function 'error'
stdin:1: in function <stdin:1>
(tail call): ?
(tail call): ?
[C]: ?
Note the hoops we have to go through to embed a statement in an
expression! It's a lot of code to be inserted for a single protected
call, and I'm sure there's a more elegant way of doing this. In
particular, this method only allows one value to be returned from the
expression.
steve d.