lua-users home
lua-l archive

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

On 10/31/07, Asko Kauppi <> 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
  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

Testing it:

$ lua  -lmacro -lmacro-defs -i
Lua 5.1.2  Copyright (C) 1994-2007, PUC-Rio
> a = {x = 2}
> = fred(a)
> = 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

steve d.